Class JerseyTest
- java.lang.Object
-
- org.glassfish.jersey.test.JerseyTest
-
- Direct Known Subclasses:
JerseyTestNg
public abstract class JerseyTest extends Object
Parent class for testing JAX-RS and Jersey-based applications using Jersey test framework.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 theTestContainer.start()
method is invoked on the configured test container. After each test method has run, theTestContainer.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 theJerseyTest
subclass can invoke theclient()
to obtain a JAX-RSClient
, from whichWebTarget
instances can be created to send arbitrary requests. Also, one of thetarget
methods (target()
ortarget(String)
) may be invoked to obtain a JAX-RSWebTarget
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 thegetTestContainerFactory()
method, then a default test container factory will be obtained as follows:-
If a system property "jersey.config.test.container.factory" is set
and the value is a fully qualified class name of a class that extends from
TestContainerFactory
then the test container factory used by default will be an instance of that class. ATestContainerException
will be thrown if the class cannot be loaded or instantiated. -
Otherwise,
META-INF/services
locations on the class path will be scanned for implementation providers ofTestContainerFactory
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. -
If no
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. ATestContainerException
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 byJerseyTest
based on the provided JAX-RS / JerseyApplication
class or instance to be tested. ATestContainerException
will be thrown if the configured test container factory cannot support the deployment context type. Two deployment context are provided:- A basic deployment context, of type
DeploymentContext
, compatible with all test containers that are not based on Servlet deployment model. - A Servlet-based deployment context, of type
ServletDeploymentContext
, compatible with Servlet-based test containers.
- Author:
- Paul Sandoz, Srinivas Bhimisetty, Pavel Bucek, Michal Gajdos, Marek Potociar
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Client
client()
Get the JAX-RS test client that ispre-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()
ReturnsTestProperties.ASYNC_TIMEOUT_MULTIPLIER
or1
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 ispre-configured
for this test.protected LogRecord
getLastLoggedRecord()
protected List<LogRecord>
getLoggedRecords()
Get storedlog records
if enabled by settingTestProperties.RECORD_LOG_LEVEL
or an empty list.protected int
getPort()
Get the port to be used for test application deployments.protected Optional<SSLContext>
getSslContext()
Return an optional instance ofSSLContext
class.protected Optional<SSLParameters>
getSslParameters()
Return an optional instance ofSSLParameters
class.protected TestContainerFactory
getTestContainerFactory()
Return an instance ofTestContainerFactory
class.protected boolean
isEnabled(String propertyName)
Check if the Jersey test boolean property (flag) has been set totrue
.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 newpre-configured
test client.WebTarget
target()
Create a JAX-RS web target whose URI refers to thebase URI
the tested JAX-RS / Jersey application is deployed at, plus the path specified in thepath
argument.WebTarget
target(String path)
Create a JAX-RS web target whose URI refers to thebase URI
the tested JAX-RS / Jersey application is deployed at, plus the path specified in thepath
argument.void
tearDown()
Tear down the test bystopping
the test container obtained from thetest container factory
and byclosing
and discarding thepre-configured
test client that wasset up
for the test.
-
-
-
Constructor Detail
-
JerseyTest
public JerseyTest()
Initialize JerseyTest instance.This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the
configure()
orconfigureDeployment()
methods to provide the tested application configuration and deployment context.
-
JerseyTest
public JerseyTest(TestContainerFactory testContainerFactory)
Initialize JerseyTest instance and specify the test container factory to be used by this test.This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the
configure()
orconfigureDeployment()
methods to provide the tested application configuration and deployment context.- Parameters:
testContainerFactory
- the test container factory to use for testing.
-
JerseyTest
public JerseyTest(Application jaxrsApplication)
Initialize JerseyTest instance.This constructor can be used from an extending subclass.
When this constructor is used, the extending concrete subclass must implement one of the
configure()
orconfigureDeployment()
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 overriddenJerseyTest.configure...
methods).- Parameters:
jaxrsApplication
- tested application.
-
-
Method Detail
-
enable
protected final void enable(String featureName)
Programmatically enable a feature with a given name. Enabling of the feature may be overridden via a system property.- Parameters:
featureName
- name of the enabled feature.
-
disable
protected final void disable(String featureName)
Programmatically disable a feature with a given name. Disabling of the feature may be overridden via a system property.- Parameters:
featureName
- name of the disabled feature.
-
forceEnable
protected final void forceEnable(String featureName)
Programmatically force-enable a feature with a given name. Force-enabling of the feature cannot be overridden via a system property. Use with care!- Parameters:
featureName
- name of the force-enabled feature.
-
forceDisable
protected final void forceDisable(String featureName)
Programmatically force-disable a feature with a given name. Force-disabling of the feature cannot be overridden via a system property. Use with care!- Parameters:
featureName
- name of the force-disabled feature.
-
set
protected final void set(String propertyName, Object value)
Programmatically set a value of a property with a given name. The property value may be overridden via a system property.- Parameters:
propertyName
- name of the property.value
- property value.
-
set
protected final void set(String propertyName, String value)
Programmatically set a value of a property with a given name. The property value may be overridden via a system property.- Parameters:
propertyName
- name of the property.value
- property value.
-
forceSet
protected final void forceSet(String propertyName, String value)
Programmatically force-set a value of a property with a given name. The force-set property value cannot be overridden via a system property.- Parameters:
propertyName
- name of the property.value
- property value.
-
isEnabled
protected final boolean isEnabled(String propertyName)
Check if the Jersey test boolean property (flag) has been set totrue
.- Parameters:
propertyName
- name of the Jersey test boolean property.- Returns:
true
if the test property has been enabled,false
otherwise.
-
configure
protected Application configure()
Create the tested JAX-RS /Jersey application.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, theconfigure()
method is invoked byconfigureDeployment()
to create default deployment context for the tested application. As such, the method is invoked in the scope of one of theJerseyTest
constructors. Default implementation of this method throwsUnsupportedOperationException
, so that construction ofJerseyTest
instance fails unless one of theconfigure()
orconfigureDeployment()
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(jakarta.ws.rs.core.Application)
constructor is used, the method is never invoked.- Returns:
- tested JAX-RS /Jersey application.
-
configureDeployment
protected DeploymentContext configureDeployment()
Create and configure deployment context for the tested application.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 createsnew deployment context
using JAX-RS application instance obtained by calling theconfigure()
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(jakarta.ws.rs.core.Application)
constructor is used, the method is never invoked.- Returns:
- configured deployment context for the tested application.
- Since:
- 2.8
-
getTestContainerFactory
protected TestContainerFactory getTestContainerFactory() throws TestContainerException
Return an instance ofTestContainerFactory
class.This method is used only once during
JerseyTest
instance construction to retrieve the factory responsible for providingTestContainer
that will be used to deploy the tested application.A default implementation first searches for the
TestContainerFactory
set viaconstructor
, then it looks for aTestContainerFactory
implementation class name set via "jersey.config.test.container.factory" system property with a fallback to searching forTestContainerFactory
service providers on the class path. At last, if noTestContainerFactory
has been found, the method attempts to create new defaultTestContainerFactory
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 fromJerseyTest
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.- Returns:
- an instance of
TestContainerFactory
class. - Throws:
TestContainerException
- if the initialization ofTestContainerFactory
instance is not successful.
-
getSslContext
protected Optional<SSLContext> getSslContext()
Return an optional instance ofSSLContext
class.This method is used only once during
JerseyTest
instance construction to retrieve the ssl configuration. By default the ssl configuration is absent, to enable it please override this method andgetSslParameters()
- Returns:
- an optional instance of
SSLContext
class.
-
getSslParameters
protected Optional<SSLParameters> getSslParameters()
Return an optional instance ofSSLParameters
class.This method is used only once during
JerseyTest
instance construction to retrieve the ssl configuration. By default the ssl configuration is absent, to enable it please override this method andgetSslContext()
()}- Returns:
- an optional instance of
SSLContext
class.
-
target
public final WebTarget target()
Create a JAX-RS web target whose URI refers to thebase URI
the tested JAX-RS / Jersey application is deployed at, plus the path specified in thepath
argument.This method is an equivalent of calling client().target(getBaseUri()).
- Returns:
- the created JAX-RS web target.
-
target
public final WebTarget target(String path)
Create a JAX-RS web target whose URI refers to thebase URI
the tested JAX-RS / Jersey application is deployed at, plus the path specified in thepath
argument.This method is an equivalent of calling
target().path(path)
.- Parameters:
path
- relative path (from tested application base URI) this web target should point to.- Returns:
- the created JAX-RS web target.
-
client
public final Client client()
Get the JAX-RS test client that ispre-configured
for this test.- Returns:
- the configured test client.
-
setUp
@BeforeEach public void setUp() throws Exception
Set up the test by creating a test container instance,starting
it and by creating a newpre-configured
test client. The test container is obtained from thetest container factory
.- Throws:
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.
-
tearDown
@AfterEach public void tearDown() throws Exception
Tear down the test bystopping
the test container obtained from thetest container factory
and byclosing
and discarding thepre-configured
test client that wasset up
for the test.- Throws:
Exception
- if an exception is thrown during tearing down the test environment.
-
getClient
protected Client getClient()
Get the JAX-RS test client that ispre-configured
for this test. This method can be overridden.- Returns:
- the configured test client.
-
setClient
protected Client setClient(Client client)
Get the old JAX-RS test client and set a new one. This method can be overridden.- Parameters:
client
- the configured test client.- Returns:
- old configured test client.
-
configureClient
protected void configureClient(ClientConfig config)
Configure the test client.The method can be overridden by
JerseyTest
subclasses to conveniently configure the test client instance used by Jersey test framework (either returned fromclient()
method or used to createWebTarget
instances returned from one of thetarget
methods (target()
ortarget(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 internalJerseyTest
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".
- Parameters:
config
- Jersey test client configuration that can be modified before the client is created.
-
getBaseUri
protected URI getBaseUri()
Returns the base URI of the tested application.- Returns:
- the base URI of the tested application.
-
getPort
protected final int getPort()
Get the port to be used for test application deployments.- Returns:
- The HTTP port of the URI
-
getLoggedRecords
protected final List<LogRecord> getLoggedRecords()
Get storedlog records
if enabled by settingTestProperties.RECORD_LOG_LEVEL
or an empty list.- Returns:
- list of log records or an empty list.
-
getLastLoggedRecord
protected final LogRecord getLastLoggedRecord()
- Returns:
- last stored
log record
ornull
.
-
getAsyncTimeoutMultiplier
protected int getAsyncTimeoutMultiplier()
ReturnsTestProperties.ASYNC_TIMEOUT_MULTIPLIER
or1
if the property is not defined.- Returns:
- Multiplier of the async timeout for async test.
-
close
public final void close(Response... responses)
Utility method that safely closes a response without throwing an exception.- Parameters:
responses
- responses to close. Each response may benull
.- Since:
- 2.5
-
closeIfNotNull
public static void closeIfNotNull(Client... clients)
Utility method that safely closes a client instance without throwing an exception.- Parameters:
clients
- client instances to close. Each instance may benull
.- Since:
- 2.5
-
-