gpt4 book ai didi

junit - CXF 单元测试

转载 作者:行者123 更新时间:2023-12-04 19:04:41 24 4
gpt4 key购买 nike

我正在使用 Apache CXF 3.0.0,并且几乎没有使用 JAX-RS 配置定义的服务。我们使用 Spring Framework 进行分层配置。这些服务的这些输入/输出是 JSON 字符串。
我正在寻找一个 Junit 测试用例的工作示例来验证我的服务。还要在 Maven Build 中配置测试。
我提到了 https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing
这是推荐的方法吗?
尽管如此,我尝试设置但无法成功,无法理解如何接线。

最佳答案

我喜欢您在链接中提到的方法,但这取决于您的设置。
我展示了如何使用 spring 配置为 cxf 服务器创建 junit 测试:

// Normal Spring Junit integration in my case with dbunit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/root-test-context.xml", "classpath:/rest-test-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup("AuthenticationResourceTest-dataset.xml")
@DatabaseTearDown("AuthenticationResourceTest-dataset.xml")
public class AuthenticationResourceTest {
// This variable is populated from surfire and reserve port maven plugin
@Value("#{systemProperties['basePath'] ?: \"http://localhost:9080/api/\"}")
private String basePath;

// I assume that you have in your spring context the rest server
@Autowired
private JAXRSServerFactoryBean serverFactory;

private Server server;

@Before
public void beforeMethod() {
serverFactory.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);
// Specify where your rest service will be deployed
serverFactory.setAddress(basePath);
server = serverFactory.create();
server.start();
}

@Test
public void authenticateTest() throws Exception {
// You can test your rest resources here.
// Using client factory
// AutenticationResourceclient = JAXRSClientFactory.create(basePath, AutenticationResource.class);
// Or URLConnection
String query = String.format("invitation=%s", URLEncoder.encode(invitation, "UTF-8"));
URL url = new URL(endpoint + "/auth?" + query);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try (InputStream is = connection.getInputStream();) {
String line;
// read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}

@After
public void afterMethod() {
server.stop();
server.destroy();
}

}

你需要在你的 maven pom.xml
    <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.2</version>
</dependency>

Plugins section:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-test-resources</phase>
<configuration>
<portNames>
<portName>test.server.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<basePath>http://localhost:${test.server.port}/api</basePath>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>

您可以从 my personal git repository 查看我的完整的例子:

关于junit - CXF 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com