public abstract class JerseyTest extends Object
At construction this class will obtain a test container factory, of type
TestContainerFactory
, and use that to obtain a configured test
container, of type TestContainer
.
Before a test method, in an extending class, is run the
TestContainer.start()
method is invoked. After the test method has
run the TestContainer.stop()
method is invoked.
The test method can invoke the resource()
to obtain a
WebResource
from which requests may 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)
) then a default
test container factory will be obtained as follows.
If the system property jersey.test.containerFactory is set and the
value is a fully qualified class name of a class that extends from
TestContainerFactory
then the default test container factory will
be an instance of that class. The exception TestContainerException
will be thrown if the class cannot be loaded or instantiated.
If the system property jersey.test.containerFactory is not set then
the default test container factory will be an instance of
.
The exception TestContainerException
will be thrown if this class
cannot be loaded or instantiated.
The test container is configured from an application descriptor, of type
AppDescriptor
. The exception TestContainerException
will be thrown if the test container cannot support the application
descriptor.
An application descriptor is built from an application descriptor builder.
Two application descriptor builders are provided:
LowLevelAppDescriptor.Builder
,
compatible with low-level test containers that do not support Servlets.WebAppDescriptor.Builder
,
compatible with web-based test containers that support Servlets.WebAppDescriptor
may be
transformed to an application descriptor of type LowLevelAppDescriptor
if the state of the former is compatible with a low-level description.
The following low-level test container factories are provided:
GrizzlyTestContainerFactory
for testing with the low-level
Grizzly HTTP container.HTTPContainerFactory
for testing with the Light Weight HTTP
server distributed with Java SE 6.InMemoryTestContainerFactory
for testing in memory without
using underlying HTTP client and server side functionality
to send requests and receive responses.GrizzlyWebTestContainerFactory
for testing with the Grizzly
Web container and Servlet support.EmbeddedGlassFishTestContainerFactory
for testing with
embedded GlassFish.ExternalTestContainerFactory
for testing when the Web
application is independently deployed in a separate JVM to that of the
tests. For example, the application may be deployed to the
Glassfish v2 or v3 application server.Constructor and Description |
---|
JerseyTest()
An extending class must implement the
configure() method to
provide an application descriptor. |
JerseyTest(AppDescriptor ad)
Construct a new instance with an application descriptor that defines
how the test container is configured.
|
JerseyTest(String... packages)
Construct a new instance with an array or a colon separated
list of package names which contain resource and provider classes.
|
JerseyTest(TestContainerFactory testContainerFactory)
Construct a new instance with a test container factory.
|
Modifier and Type | Method and Description |
---|---|
Client |
client()
Get the client that is configured for this test.
|
protected AppDescriptor |
configure()
Return an application descriptor that defines how the test container
is configured.
|
protected URI |
getBaseURI()
Returns the base URI of the application.
|
protected Client |
getClient(TestContainer tc,
AppDescriptor ad)
Creates an instance of
Client . |
protected ClientFactory |
getClientFactory()
Get the ClientFactory.
|
protected int |
getPort(int defaultPort)
Returns the port to be used in the base URI.
|
protected TestContainerFactory |
getTestContainerFactory()
Get the test container factory.
|
WebResource |
resource()
Create a web resource whose URI refers to the base URI the Web
application is deployed at.
|
protected void |
setClientFactory(ClientFactory clientFactory) |
protected void |
setTestContainerFactory(TestContainerFactory testContainerFactory)
Sets the test container factory to to be used for testing.
|
void |
setUp()
Set up the test by invoking
TestContainer.start() on
the test container obtained from the test container factory. |
void |
tearDown()
Tear down the test by invoking
TestContainer.stop() on
the test container obtained from the test container factory. |
public JerseyTest() throws TestContainerException
configure()
method to
provide an application descriptor.TestContainerException
- if the default test container factory
cannot be obtained, or the application descriptor is not
supported by the test container factory.public JerseyTest(TestContainerFactory testContainerFactory)
An extending class must implement the configure()
method to
provide an application descriptor.
testContainerFactory
- the test container factory to use for testing.TestContainerException
- if the application descriptor is not
supported by the test container factory.public JerseyTest(AppDescriptor ad) throws TestContainerException
ad
- an application descriptor describing how to configure the
test container.TestContainerException
- if the default test container factory
cannot be obtained, or the application descriptor is not
supported by the test container factory.public JerseyTest(String... packages) throws TestContainerException
This constructor builds an instance of WebAppDescriptor
passing
the package names to the constructor.
packages
- array or a colon separated list of package names which
contain resource and provider classes.TestContainerException
- if the default test container factory
cannot be obtained, or the built application descriptor is not
supported by the test container factory.protected AppDescriptor configure()
If a constructor is utilized that does not supply an application
descriptor then this method must be overridden to return an application
descriptor, otherwise an UnsupportedOperationException
exception
will be thrown.
If a constructor is utilized that does supply an application descriptor then this method does not require to be overridden and will not be invoked.
protected void setTestContainerFactory(TestContainerFactory testContainerFactory)
testContainerFactory
- the test container factory to to be used for
testing.protected TestContainerFactory getTestContainerFactory() throws TestContainerException
If the test container factory has not been explicit set with
setTestContainerFactory(TestContainerFactory)
then
the default test container factory will be obtained.
If the system property jersey.test.containerFactory is set and the
value is a fully qualified class name of a class that extends from
TestContainerFactory
then the default test container factory will
be an instance of that class. The exception TestContainerException
will be thrown if the class cannot be loaded or instantiated.
If the system property jersey.test.containerFactory is not set then
the default test container factory will be an instance of
.
The exception TestContainerException
will be thrown if this class
cannot be loaded or instantiated.
TestContainerException
- if the default test container factory
cannot be obtained.public WebResource resource()
public Client client()
public void setUp() throws Exception
TestContainer.start()
on
the test container obtained from the test container factory.Exception
public void tearDown() throws Exception
TestContainer.stop()
on
the test container obtained from the test container factory.Exception
protected Client getClient(TestContainer tc, AppDescriptor ad)
Client
.
Checks whether TestContainer provides client instance and
if not, ClientFactory obtained through getClientFactory()
will be used to create new client instance.
This method is called exactly once when JerseyTest is created.tc
- instance of TestContainer
ad
- instance of AppDescriptor
protected ClientFactory getClientFactory()
If the client factory has not been explicit set with
setClientFactory(ClientFactory)
then
the default client factory will be obtained.
protected void setClientFactory(ClientFactory clientFactory)
protected URI getBaseURI()
protected int getPort(int defaultPort)
defaultPort
- default portCopyright © 2013 Oracle Corporation. All rights reserved.