开发者

how to test an REST app (built with apache-cxf) with jersey-test-framework

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>
    &l开发者_Python百科t;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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜