public abstract class JerseyTest5 extends Object
At construction this class will obtain a test container
factory
implementation.
Before each test method in an extending class is run the test container factory is used to obtain a configured
test container
. Then the TestContainer.start()
method is
invoked on the configured test container. After each test method has run, the TestContainer.stop()
method is
invoked on the test container. Stopped test container generally shouldn't be again started for another test, rather a
new test container should be created. Every test method in the JerseyTest
subclass can invoke the
client()
to obtain a JAX-RS Client
, from which
WebTarget
instances can be created to send arbitrary requests. Also, one of the
target
methods (target()
or target(String)
) may be invoked to obtain a JAX-RS
WebTarget
instances from which requests can be sent to and responses received from the Web
application under test.
If a test container factory is not explicitly declared using the appropriate constructor (see
JerseyTest5(TestContainerFactory)
) or by overriding the getTestContainerFactory()
method, then a
default test container factory will be obtained as follows:
TestContainerFactory
then the test
container factory used by default will be an instance of that class. A TestContainerException
will be thrown
if the class cannot be loaded or instantiated.META-INF/services
locations on the class path will be scanned for implementation providers of
TestContainerFactory
SPI. If a single implementation is found, it will be used. If multiple implementations
are found, the default "jersey.config.test.container.factory" implementation
will be selected if present, otherwise the first found implementation will be selected and a warning message will be
logged.TestContainerFactory
has been selected in the steps above, Jersey test framework will try to
instantiate the default test container factory implementation (
"org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory") directly. A
TestContainerException
will be thrown if this class cannot be loaded or instantiated.
The test container is configured by a DeploymentContext
that is either provided by subclass or automatically
created by JerseyTest
based on the provided JAX-RS / Jersey Application
class or instance to be
tested. A TestContainerException
will be thrown if the configured test container factory cannot support the
deployment context type. Two deployment context are provided:
DeploymentContext
, compatible with all test containers that are not
based on Servlet deployment model.ServletDeploymentContext
, compatible with Servlet-based test
containers.Constructor and Description |
---|
JerseyTest5()
Initialize JerseyTest instance.
|
JerseyTest5(javax.ws.rs.core.Application jaxrsApplication)
Initialize JerseyTest instance.
|
JerseyTest5(org.glassfish.jersey.test.spi.TestContainerFactory testContainerFactory)
Initialize JerseyTest instance and specify the test container factory to be used by this test.
|
Modifier and Type | Method and Description |
---|---|
javax.ws.rs.client.Client |
client()
Get the JAX-RS test client that is
pre-configured for this test. |
static void |
close(javax.ws.rs.core.Response... responses)
Utility method that safely closes a response without throwing an exception.
|
static void |
closeIfNotNull(javax.ws.rs.client.Client... clients)
Utility method that safely closes a client instance without throwing an exception.
|
void |
setUp()
Set up the test by creating a test container instance,
starting it and by creating
a new pre-configured test client. |
javax.ws.rs.client.WebTarget |
target()
Create a JAX-RS web target whose URI refers to the
base URI the tested JAX-RS / Jersey
application is deployed at, plus the path specified in the path argument. |
javax.ws.rs.client.WebTarget |
target(String path)
Create a JAX-RS web target whose URI refers to the
base URI the tested JAX-RS / Jersey
application is deployed at, plus the path specified in the path argument. |
void |
tearDown()
Tear down the test by
stopping the test container obtained from the
test container factory and by closing and discarding the pre-configured
test client that was set up for the test. |
public JerseyTest5()
This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the configure()
or
configureDeployment()
methods to provide the tested application configuration and deployment context.
public JerseyTest5(org.glassfish.jersey.test.spi.TestContainerFactory testContainerFactory)
This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the configure()
or
configureDeployment()
methods to provide the tested application configuration and deployment context.
testContainerFactory
- the test container factory to use for testing.public JerseyTest5(javax.ws.rs.core.Application jaxrsApplication)
This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the configure()
or
configureDeployment()
methods are ignored.
Please note that when this constructor is used, recording of startup logs as well as configuring other
JerseyTest
properties and features may not work properly. While using this constructor should generally
be avoided, in certain scenarios it may be necessary to use this constructor. (E.g. when running parameterized
tests in which application is created based on test parameters passed in by JUnit framework via test constructor
- in such case it is not possible to propagate the necessary information to one of the overridden
JerseyTest.configure...
methods).
jaxrsApplication
- tested application.public final javax.ws.rs.client.WebTarget target()
base URI
the tested JAX-RS / Jersey
application is deployed at, plus the path specified in the path
argument.
This method is an equivalent of calling client().target(getBaseUri()).
public final javax.ws.rs.client.WebTarget target(String path)
base URI
the tested JAX-RS / Jersey
application is deployed at, plus the path specified in the path
argument.
This method is an equivalent of calling target().path(path)
.
path
- relative path (from tested application base URI) this web target should point to.public final javax.ws.rs.client.Client client()
pre-configured
for this test.@BeforeEach public void setUp() throws Exception
starting
it and by creating
a new pre-configured
test client. The test
container is obtained from the test container factory
.org.glassfish.jersey.test.spi.TestContainerException
- if the default test container factory cannot be obtained, or the test application
deployment context is not supported by the test container factory.Exception
- if an exception is thrown during setting up the test environment.@AfterEach public void tearDown() throws Exception
stopping
the test container obtained from the
test container factory
and by closing
and discarding the pre-configured
test client that was set up
for the test.Exception
- if an exception is thrown during tearing down the test environment.public static final void close(javax.ws.rs.core.Response... responses)
responses
- responses to close. Each response may be null
.public static void closeIfNotNull(javax.ws.rs.client.Client... clients)
clients
- client instances to close. Each instance may be null
.Copyright © 2019 The Holon Platform. All rights reserved.