JAX-RS - Unit Testing Using Jersey Test Framework

[Updated: Feb 21, 2016, Created: Jan 22, 2016]

Jersey provides a test framework to test our JAX-RS application without running an application/servlet container. It also supports running the unit tests against any external container as well. Here we are going to give quick example how we can do that.

  1. Like our previous examples add jersey main dependencies in pom.xml. This time we are going to add a new one:

  2. Create our service class :

    package com.logicbig.example;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    public class OrderService {
        public String getOrders(@PathParam("orderId") String orderId) {
            return "orderId: " + orderId;
        public String getOrdersSummary() {
            return "orders summary";
  3. Create our RestApplicaiton class extending Application. This class is not used by the Jersey Test Framework though. But we are still adding it so that we can run our app normally.

    package com.logicbig.example;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    public class RestApplication extends Application {
  4. Create our unit test class.

    package com.logicbig.example;
    import org.glassfish.jersey.server.ResourceConfig;
    import org.glassfish.jersey.test.JerseyTest;
    import org.junit.Assert;
    import org.junit.Test;
    import javax.ws.rs.core.Application;
    public class OrderServiceTest extends JerseyTest {
        protected Application configure() {
            return new ResourceConfig(OrderService.class);
        public void ordersPathParamTest() {
            String response = target("orders/453").request().get(String.class);
            Assert.assertTrue("orderId: 453".equals(response));
        public void ordersFixedPathTest() {
            String response = target("orders/summary").request().get(String.class);
            Assert.assertTrue("orders summary".equals(response));

    In above code, we are extending our test class from JerseyTest which provides many methods to be called from sub classes to write our application specific test code.

    ResourceConfig is a sub-class of JAX-RS Application. It is a Jersey specific class for configuring JAX-RS applications. That means tests will run an internal container with JAX-RS Application.

    Typically we override configure() method to reutrn a new instance of ResourceConfig.

    Super class method call target(..) returns an instance of javax.ws.rs.client.WebTarget, which is a JAX-RS Client API class. From that point on we use the standard JAX-RS client API and JUnit asserts to perform tests.

Example project

We can run tests from our IDE or with mvn:

mvn test

We may also want to run application using embedded tomcat server:

mvn clean tomcat7:run-war

Use this url in your browser:


Dependencies and Technologies Used :

  • jersey-core-server 2.22.1: Jersey core server implementation.
  • jersey-container-servlet 2.22.1: Jersey core Servlet 3.x implementation.
  • jersey-test-framework-provider-jdk-http 2.22.1: Jersey Test Framework - JDK HTTP container.
  • JDK 1.8
  • Maven 3.0.4

Jersey Unit Text Example Select All Download
  • jersey-unit-test-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example

See Also