如何使用 jersey-test-framework 测试 REST 应用程序(使用 apache-cxf 构建)
我想知道如何使用 Jersey Test Framework (JTF) 测试使用 Apache CXF 和 Spring 开发的 REST 应用程序。
该应用程序由多个 Maven 模块组成,其中“app-rest”是集成它们并公开要测试的 REST 接口的模块。
我制作了一个包含测试的单独 Maven 模块,该模块具有“app-rest”作为依赖项,但我收到如下异常:
BeanDefinitionStoreException: IOException parsing XML document from class path resource
运行测试时。
我认为这是因为 app-rest 没有正确部署在嵌入式容器中。
我试图将测试放入“app-rest”模块中,但我得到的是:
RuntimeException: The scope of the component class org.apache.cxf.jaxrs.provider.AtomFeedProvider must be a singleton
我正在使用此命令行运行测试:
mvn test -Dtest=JerseyRestTest -Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory
这是我的测试模块的 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>project-group-id</groupId>
<artifactId>projectname</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>project-group-id</groupId>
<artifactId>integration-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Integration Tests of REST interface</name>
<!-- build -->
<build>
<finalName>${project.artifactId}</finalName>
</build>
<!-- additional repositories -->
<repositories>
<repository>
<id>java.net.m2</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>project-group-id</groupId>
<artifactId>app-rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Jersey stuff -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
简单的测试是:
public class JerseyRestTest extends JerseyTest {
public JerseyRestTest() {
super(new WebAppDescriptor.Builder("projectname.resource")
.servletClass(SpringServlet.class)
.contextParam("contextConfigLocation", "classpath:META-INF/spring/context-rest.xml")
.contextListenerClass(org.springframework.web.context.ContextLoaderListener.class)
.contextPath("app-rest")
.build());
}
@Test
public void testSomeResource() {
String response = resource().path("/rest/resources").get(String.class);
Assert.assertNotNull("No text returned!", response);
assertResponseContains(response, "<html>");
assertResponseContains(response, "</html>");
}
protected void assertResponseContains(String response, String text) {
Assert.assertTrue("Response should contain " + text + " but was: " + response, response.contains(text));
}
}
非常感谢!
I would like to know how I can test an REST application, which is developed using Apache CXF and Spring, with the Jersey Test Framework (JTF).
The application is made of several Maven modules, of which "app-rest" is the one that integrates them and exposes the REST interface to be tested.
I've made a separate Maven module that contains the tests, which has the "app-rest" as dependency, but I'm receiving an exception like this :
BeanDefinitionStoreException: IOException parsing XML document from class path resource
when running the tests.
I think that's because the app-rest is not properly deployed in the embedded container.
I've tried to put the tests into the "app-rest" module, but I get this instead :
RuntimeException: The scope of the component class org.apache.cxf.jaxrs.provider.AtomFeedProvider must be a singleton
I'm running the test with this command line:
mvn test -Dtest=JerseyRestTest -Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory
These is my pom.xml file for the tests module:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>project-group-id</groupId>
<artifactId>projectname</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>project-group-id</groupId>
<artifactId>integration-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Integration Tests of REST interface</name>
<!-- build -->
<build>
<finalName>${project.artifactId}</finalName>
</build>
<!-- additional repositories -->
<repositories>
<repository>
<id>java.net.m2</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>project-group-id</groupId>
<artifactId>app-rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Jersey stuff -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The simple test is:
public class JerseyRestTest extends JerseyTest {
public JerseyRestTest() {
super(new WebAppDescriptor.Builder("projectname.resource")
.servletClass(SpringServlet.class)
.contextParam("contextConfigLocation", "classpath:META-INF/spring/context-rest.xml")
.contextListenerClass(org.springframework.web.context.ContextLoaderListener.class)
.contextPath("app-rest")
.build());
}
@Test
public void testSomeResource() {
String response = resource().path("/rest/resources").get(String.class);
Assert.assertNotNull("No text returned!", response);
assertResponseContains(response, "<html>");
assertResponseContains(response, "</html>");
}
protected void assertResponseContains(String response, String text) {
Assert.assertTrue("Response should contain " + text + " but was: " + response, response.contains(text));
}
}
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论