Search code examples
web-servicesunit-testingjersey-2.0

how to perform unit testing jersey webservice


Actully I am trying to test my Jersey web service with the jersey test framework. the web server I am using is Websphere 7 and java version 6. This is my project requirement I cannot upgrade the java version .

My issue is how to build the unit test for my web service . I want to test them on WebSphere but I am not sure how do I setup the environment for unit testing like junit .

more specifically I just need to call an URL from the test class and check the response. But how to call the URL from the test class on the websphere I am not getting direction for it.


Solution

  • Have a look at the documentation for Jersey Test Framework.

    First thing you need is one of the Supported Containers dependencies. Any of those will pull in the core framework e.g.

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>2.19</version>
        <scope>test</scope>
    </dependency>
    

    Then you need a test class that extends JerseyTest. You can override Application configure() to provide a ResourceConfig as well as any other providers or properties. For example

    @Path("/test")
    public class TestResource {
        @GET
        public String get() { return "hello"; }
    }
    
    public class TestResourceTest extends JerseyTest {
    
        @Override
        public Application configure() {
            ResourceConfig config = new ResourceConfig();
            config.register(TestResource.class);
        }
    
        @Test
        public void doTest() {
            Response response = target("test").request().get();
            assertEquals(200, response.getStatus());
            assertEquals("hello", response.readEntity(String.class));
        }
    }
    

    You should visit the link provided to learn more and see more examples.