Skip navigation links

Package org.glassfish.jersey.test

Jersey test framework common classes that support testing JAX-RS and Jersey-based applications.

See: Description

Package org.glassfish.jersey.test Description

Jersey test framework common classes that support testing JAX-RS and Jersey-based applications. The JerseyTest class may be extended to define the testing configuration and functionality.

For example, the following class is configured to use Grizzly HTTP test container factory, org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory and test that a simple resource TestResource returns the expected results for a HTTP GET request:

 public class SimpleGrizzlyBasedTest extends JerseyTest {

   @Path("root")
   public static class TestResource {
     @GET
     public String get() {
       return "GET";
     }
   }

   @Override
   protected Application configure() {
     enable(TestProperties.LOG_TRAFFIC);
     return new ResourceConfig(TestResource.class);
   }

   @Override
   protected TestContainerFactory getTestContainerFactory() {
     return new GrizzlyTestContainerFactory();
   }

   @Test
   public void testGet() {
     WebTarget t = target("root");

     String s = t.request().get(String.class);
     Assert.assertEquals("GET", s);
   }
 }
 

The following tests the same functionality using the Servlet-based Grizzly test container factory, org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory:

 public class WebBasedTest extends JerseyTest {

   @Path("root")
   public static class TestResource {
     @GET
     public String get() {
       return "GET";
     }
   }

   @Override
   protected DeploymentContext configureDeployment() {
     return ServletDeploymentContext.builder("foo")
             .contextPath("context").build();
   }

   @Override
   protected TestContainerFactory getTestContainerFactory() {
     return new GrizzlyTestContainerFactory();
   }

   @Test
   public void testGet() {
     WebTarget t = target("root");

     String s = t.request().get(String.class);
     Assert.assertEquals("GET", s);
   }
 }
 

The above test is actually not specific to any Servlet-based test container factory and will work for all provided test container factories. See the documentation on JerseyTest for more details on how to set the default test container factory.

Skip navigation links

Copyright © 2007-2015, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.