com.sun.jersey.test.framework
Class JerseyTest

java.lang.Object
  extended by com.sun.jersey.test.framework.JerseyTest

public abstract class JerseyTest
extends java.lang.Object

An abstract JUnit 4.x-based unit test class for testing JAX-RS and Jersey-based applications.

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 recieved from the Web application under test.

If a test container factory is not explictly declared using the appropriate constructor (see JerseyTest(TestContainerFactory)) then a default test container factory will be obtained as follows. If the system property 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 test.containerFactory is not set then the default test container factory will be an instance of GrizzlyWebTestContainerFactory. 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:

  1. A low-level builder, of type LowLevelAppDescriptor.Builder, compatible with low-level test containers that do not support Servlets.
  2. A web-based builder, of type WebAppDescriptor.Builder, compatible with web-based test containers that support Servlets.
An application descriptor of type 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:

The following Web-based test container factories are provided:

Author:
Paul.Sandoz@Sun.COM, Srinivas.Bhimisetty@Sun.COM

Constructor Summary
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(java.lang.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)
          Contruct a new instance with a test container factory.
 
Method Summary
 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  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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JerseyTest

public JerseyTest()
           throws TestContainerException
An extending class must implement the configure() method to provide an application descriptor.

Throws:
TestContainerException - if the default test container factory cannot be obtained, or the application descriptor is not supported by the test container factory.

JerseyTest

public JerseyTest(TestContainerFactory testContainerFactory)
Contruct a new instance with a test container factory.

An extending class must implement the configure() method to provide an application descriptor.

Parameters:
testContainerFactory - the test container factory to use for testing.
Throws:
TestContainerException - if the application descriptor is not supported by the test container factory.

JerseyTest

public JerseyTest(AppDescriptor ad)
           throws TestContainerException
Construct a new instance with an application descriptor that defines how the test container is configured.

Parameters:
ad - an application descriptor describing how to configure the test container.
Throws:
TestContainerException - if the default test container factory cannot be obtained, or the application descriptor is not supported by the test container factory.

JerseyTest

public JerseyTest(java.lang.String... packages)
           throws TestContainerException
Construct a new instance with an array or a colon separated list of package names which contain resource and provider classes.

This contructor builds an instance of WebAppDescriptor passing the package names to the constructor.

Parameters:
packages - array or a colon separated list of package names which contain resource and provider classes.
Throws:
TestContainerException - if the default test container factory cannot be obtained, or the built application descriptor is not supported by the test container factory.
Method Detail

configure

protected AppDescriptor configure()
Return an application descriptor that defines how the test container is configured.

If a constructor is utilized that does not supply an application descriptor then this method must be overriden 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.

Returns:
the application descriptor.

setTestContainerFactory

protected void setTestContainerFactory(TestContainerFactory testContainerFactory)
Sets the test container factory to to be used for testing.

Parameters:
testContainerFactory - the test container factory to to be used for testing.

getTestContainerFactory

protected TestContainerFactory getTestContainerFactory()
                                                throws TestContainerException
Get the test container factory.

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 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 test.containerFactory is not set then the default test container factory will be an instance of GrizzlyWebTestContainerFactory. The exception TestContainerException will be thrown if this class cannot be loaded or instantiated.

Returns:
the test container factory.
Throws:
TestContainerException - if the default test container factory cannot be obtained.

resource

public WebResource resource()
Create a web resource whose URI refers to the base URI the Web application is deployed at.

Returns:
the created web resource

client

public Client client()
Get the client that is configured for this test.

Returns:
the configured client.

setUp

public void setUp()
           throws java.lang.Exception
Set up the test by invoking TestContainer.start() on the test container obtained from the test container factory.

Throws:
java.lang.Exception

tearDown

public void tearDown()
              throws java.lang.Exception
Tear down the test by invoking TestContainer.stop() on the test container obtained from the test container factory.

Throws:
java.lang.Exception


Copyright © 2009 Sun Microsystems, Inc. All Rights Reserved.