Package org.glassfish.jersey.test
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); Assertions.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); Assertions.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.
-
Class Summary Class Description ContainerPerClassTestNgStrategy TestNG strategy that creates one test container / client per test class.ContainerPerMethodTestNgStrategy TestNG strategy that creates separate test container / client per test method.DeploymentContext Basic application deployment context.DeploymentContext.Builder Deployment context builder for building a basic application deployment context for the JAX-RS / Jersey application defined by the associatedApplication
class or instance.JerseyTest Parent class for testing JAX-RS and Jersey-based applications using Jersey test framework.JerseyTestNg Parent class for testing JAX-RS and Jersey-based applications using Jersey test framework and TestNG framework.JerseyTestNg.ContainerPerClassTest Parent for TestNg tests that needs to create a test container only once per a test class.JerseyTestNg.ContainerPerMethodTest Parent for TestNg tests that needs to create a separate test container for each test in a test class.ServletDeploymentContext A Servlet-based deployment context.ServletDeploymentContext.Builder The builder for building a Servlet-based deployment context.ServletDeploymentContext.FilterDescriptor Helper class to keep configuration information of a single filter.TestProperties Jersey test framework configuration properties.