public abstract class JerseyTest 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 JerseyTest(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 |
---|
JerseyTest()
Initialize JerseyTest instance.
|
JerseyTest(Application jaxrsApplication)
Initialize JerseyTest instance.
|
JerseyTest(TestContainerFactory testContainerFactory)
Initialize JerseyTest instance and specify the test container factory to be used by this test.
|
Modifier and Type | Method and Description |
---|---|
Client |
client()
Get the JAX-RS test client that is
pre-configured
for this test. |
void |
close(Response... responses)
Utility method that safely closes a response without throwing an exception.
|
static void |
closeIfNotNull(Client... clients)
Utility method that safely closes a client instance without throwing an exception.
|
protected Application |
configure()
Create the tested JAX-RS /Jersey application.
|
protected void |
configureClient(ClientConfig config)
Configure the test client.
|
protected DeploymentContext |
configureDeployment()
Create and configure deployment context for the tested application.
|
protected void |
disable(String featureName)
Programmatically disable a feature with a given name.
|
protected void |
enable(String featureName)
Programmatically enable a feature with a given name.
|
protected void |
forceDisable(String featureName)
Programmatically force-disable a feature with a given name.
|
protected void |
forceEnable(String featureName)
Programmatically force-enable a feature with a given name.
|
protected void |
forceSet(String propertyName,
String value)
Programmatically force-set a value of a property with a given name.
|
protected int |
getAsyncTimeoutMultiplier()
Returns
TestProperties.ASYNC_TIMEOUT_MULTIPLIER or 1 if the property is not defined. |
protected URI |
getBaseUri()
Returns the base URI of the tested application.
|
protected Client |
getClient()
Get the JAX-RS test client that is
pre-configured
for this test. |
protected LogRecord |
getLastLoggedRecord()
|
protected List<LogRecord> |
getLoggedRecords()
Get stored
log records if enabled by setting TestProperties.RECORD_LOG_LEVEL or an empty list. |
protected int |
getPort()
Get the port to be used for test application deployments.
|
protected TestContainerFactory |
getTestContainerFactory()
Return an instance of
TestContainerFactory class. |
protected boolean |
isEnabled(String propertyName)
Check if the Jersey test boolean property (flag) has been set to
true . |
protected void |
set(String propertyName,
Object value)
Programmatically set a value of a property with a given name.
|
protected void |
set(String propertyName,
String value)
Programmatically set a value of a property with a given name.
|
protected Client |
setClient(Client client)
Get the old JAX-RS test client and set a new one.
|
void |
setUp()
Set up the test by creating a test container instance,
starting it and by creating a new
pre-configured test 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. |
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 JerseyTest()
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 JerseyTest(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 JerseyTest(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.protected final void enable(String featureName)
featureName
- name of the enabled feature.protected final void disable(String featureName)
featureName
- name of the disabled feature.protected final void forceEnable(String featureName)
featureName
- name of the force-enabled feature.protected final void forceDisable(String featureName)
featureName
- name of the force-disabled feature.protected final void set(String propertyName, Object value)
propertyName
- name of the property.value
- property value.protected final void set(String propertyName, String value)
propertyName
- name of the property.value
- property value.protected final void forceSet(String propertyName, String value)
propertyName
- name of the property.value
- property value.protected final boolean isEnabled(String propertyName)
true
.propertyName
- name of the Jersey test boolean property.true
if the test property has been enabled, false
otherwise.protected Application configure()
This method may be overridden by subclasses to provide the configured JAX-RS /Jersey application to be tested.
The method may be also used to configure JerseyTest
instance properties.
Unless configureDeployment()
method is overridden in the subclass, the configure()
method is invoked
by configureDeployment()
to create default deployment context for the tested application. As such, the method
is invoked in the scope of one of the JerseyTest
constructors.
Default implementation of this method throws UnsupportedOperationException
, so that construction of
JerseyTest
instance fails unless one of the configure()
or configureDeployment()
methods is
overridden in the subclass.
Note that since the method is invoked from JerseyTest
constructor, the overriding implementation of the method
must not depend on any subclass fields as those will not be initialized yet when the method is invoked.
Also note that in case the JerseyTest(javax.ws.rs.core.Application)
constructor is used, the method is never
invoked.
protected DeploymentContext configureDeployment()
This method may be overridden by subclasses to provide custom test container deployment context for the tested
application. The method may be also used to configure JerseyTest
instance properties.
The method is invoked from JerseyTest
constructors to provide deployment context for the tested application.
Default implementation of this method creates
new deployment context
using JAX-RS application instance obtained by calling the configure()
method.
Note that since the method is invoked from JerseyTest
constructor, the overriding implementation of the method
must not depend on any subclass fields as those will not be initialized yet when the method is invoked.
Also note that in case the JerseyTest(javax.ws.rs.core.Application)
constructor is used, the method is never
invoked.
protected TestContainerFactory getTestContainerFactory() throws TestContainerException
TestContainerFactory
class.
This method is used only once during JerseyTest
instance construction to retrieve the factory responsible
for providing TestContainer
that will be used to deploy the tested application.
A default implementation first searches for the TestContainerFactory
set via
constructor
, then it looks for a
TestContainerFactory
implementation class name set via
"jersey.config.test.container.factory" system property with a fallback to
searching for TestContainerFactory
service providers on the class path. At last, if no
TestContainerFactory
has been found, the method attempts to create new default
TestContainerFactory
implementation instance
("org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory").
Alternatively, this method may be overridden to directly provide a custom TestContainerFactory
instance.
Note that since the method is invoked from JerseyTest
constructor, the overriding implementation of the method
must not depend on any subclass fields as those will not be initialized yet when the method is invoked.
TestContainerFactory
class.TestContainerException
- if the initialization of TestContainerFactory
instance is not successful.public final 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 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 Client client()
pre-configured
for this test.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
.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.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.protected Client getClient()
pre-configured
for this test. This method can be overridden.protected Client setClient(Client client)
client
- the configured test client.protected void configureClient(ClientConfig config)
The method can be overridden by JerseyTest
subclasses to conveniently configure the test client instance
used by Jersey test framework (either returned from client()
method or used to create
WebTarget
instances returned from one of the target
methods
(target()
or target(String)
).
Prior to every test method run, a new client instance is configured and created using the client configuration
provided by the TestContainer
as well as any internal JerseyTest
client configuration settings.
Before the actual client instance creation, Jersey test framework invokes this method in order to allow the subclasses to further customize created client instance.
After each test method is run, the existing client instance is closed
and discarded.
Default implementation of the method is "no-op".
config
- Jersey test client configuration that can be modified before the client is created.protected URI getBaseUri()
protected final int getPort()
protected final List<LogRecord> getLoggedRecords()
log records
if enabled by setting TestProperties.RECORD_LOG_LEVEL
or an empty list.protected final LogRecord getLastLoggedRecord()
log record
or null
.protected int getAsyncTimeoutMultiplier()
TestProperties.ASYNC_TIMEOUT_MULTIPLIER
or 1
if the property is not defined.public final void close(Response... responses)
responses
- responses to close. Each response may be null
.public static void closeIfNotNull(Client... clients)
clients
- client instances to close. Each instance may be null
.Copyright © 2007-2017, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.