如何使用jersey-test-framework测试REST应用程序(使用apache-cxf构建)

时间:2011-05-11 14:23:06

标签: testing jersey cxf


我想知道如何使用Apache测试框架(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));
    }
}

非常感谢!

0 个答案:

没有答案