Unit testing jersey Restful Services

51,051

Solution 1

For Jersey web services testing there are several testing frameworks, namely: Jersey Test Framework (already mentioned in other answer - see here documentation for version 1.17 here: https://jersey.java.net/documentation/1.17/test-framework.html) and REST-Assured (https://code.google.com/p/rest-assured) - see here a comparison/setup of both (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/).

I find the REST-Assured more interesting and powerful, but Jersey Test Framework is very easy to use too. In REST-Assured to write a test case "to check response status and json format" you could write the following test (very much like you do in jUnit):

package com.example.rest;

import static com.jayway.restassured.RestAssured.expect;
import groovyx.net.http.ContentType;

import org.junit.Before;
import org.junit.Test;

import com.jayway.restassured.RestAssured;

public class Products{

    @Before
    public void setUp(){
        RestAssured.basePath = "http://localhost:8080";
    }

    @Test
    public void testGetProducts(){
        expect().statusCode(200).contentType(ContentType.JSON).when()
                .get("/getProducts/companyid/companyname/12345088723");
    }

}

This should do the trick for you... you can verify JSON specific elements also very easily and many other details. For instructions on more features you can check the very good guide from REST-Assured (https://code.google.com/p/rest-assured/wiki/Usage). Another good tutorial is this one http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/.

HTH.

Solution 2

Just ignore the annotations and write a normal unit test that passes the required parameters. The return I thought would usually be of type "javax.ws.rs.core.Response" ... There is a getEntity() method on that can be used. Using a Mock object framework like Mockito could be helpful in this case too.

Solution 3

Are you familiar with Chapter 26. Jersey Test Framework?

public class SimpleTest extends JerseyTest {

    @Path("hello")
    public static class HelloResource {
        @GET
        public String getHello() {
            return "Hello World!";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(HelloResource.class);
    }

    @Test
    public void test() {
        final String hello = target("hello").request().get(String.class);
        assertEquals("Hello World!", hello);
    }
}
Share:
51,051
Kiran
Author by

Kiran

Updated on January 09, 2020

Comments

  • Kiran
    Kiran over 4 years

    I'm new to unit testing and I want to test some jersey services in a project. We are using Junit. Please guide me to write test cases in better way.

    CODE:

        @GET
        @Path("/getProducts/{companyID}/{companyName}/{date}")
        @Produces(MediaType.APPLICATION_JSON)
        public Object getProducts(@PathParam("companyID") final int companyID,
                @PathParam("date") final String date, @PathParam("companyName") final String companyName)
                throws IOException {
            return productService.getProducts(companyID, companyName, date);
        }
    

    Above mentioned service is working fine and I want to write junit test case to test above mentioned method. Above method will retrieve list of products (List<Product>) in JSON format. I would like to write test case to check response status and json format.

    NOTE: We are using Jersey 1.17.1 version.

    Help would be appreciated :)

  • Kiran
    Kiran over 10 years
    @Mariuszs.. Thanks for your reply bro. But we are using jersey 1.17.1 version which will not support Application and ResourceConfig. Could you please share 1.17.1 example if you have?
  • Kiran
    Kiran over 10 years
    @Neeraj.. Thanks for your reply. Could you please share sample if you have?
  • Michael Piefel
    Michael Piefel almost 10 years
    While you easily get 100% coverage with this method, you usually get little knowledge about your software. This is a pure unit test, which has its value, but you might want to test at least some integration alongside, such as URL parsing and data marshalling.
  • emgsilva
    emgsilva over 9 years
    This is just an example... you can specify the address of the API endpoint you need.
  • ACV
    ACV almost 8 years
    Actually only this worked for me: @Before public void setUp() { RestAssured.baseURI = "http://localhost:8080/"; RestAssured.basePath = "/appname"; }
  • Alex Punnen
    Alex Punnen over 7 years
    Note - RestAssured.baseURI should be set first james-willett.com/2015/06/…
  • Prathik Rajendran M
    Prathik Rajendran M almost 7 years
    Downvoting as this is not a unit test. It can be modified to one but the right way to do DI is not shown here,