Jersey Test Framework makes it easy!

   Does your application have RESTful Web Services? Do you want to ensure that these services are working properly on a wide range of containers - both light weight and heavy weight ones?

     Have you ever felt the need of an infrastructure setup, which you can use to test your services against all these containers without having to worry about things like deployment descriptors, etc? If so, you have a news. Jersey 1.0.3 got released day before yesterday, and it comes with a testing framework called the Jersey Test Framework

     The Jersey Test Framework currently allows you to run your tests on any of the following three light weight containers:

The framework is built over JUnit 4.x using Maven.

How do I use the Jersey Test Framework?

          Using the framework is simple. All that you will need  is do this:

  1. Add the following dependency to your pom.xml:
    • <dependency>
                  <groupId>com.sun.jersey.test.framework</groupId>
                  <artifactId>jersey-test-framework</artifactId>
                  <version>1.0.3</version>
                  <scope>test</scope>
       </dependency>
  2. Create a class which extends com.sun.jersey.test.framework.JerseyTest.
  3. Some minimal number of parameters need to be passed by the test class to the JerseyTest class. This can be done in one of the following ways:
    • super(String rootResourcePackage): Pass the root resource package name to the super constructor. This constructor will then take care of initialising, starting and/or stopping the test container.
    • super(String contextPath, String servletPath, String resourcePackageName): Pass the application context path, servlet path and root resource package name to the super constructor if you are working on a web application. Again this constructor will take care of initializing, starting and/or stopping the test container.
    • super(): When you call the default no parameter super constructor, you still can pass the information to the JerseyTest class by creating an instance of the com.sun.jersey.test.framework.util.ApplicationDescriptor class, setting the parameters using the setter methods defined in that class. This has to be done in your test class's constructor. Also, a call needs to be made to the JerseyTest class's setupTestEnvironment(ApplicationDescriptor applicationDescriptor) method. This call would take care of the init, start and/stop of the test container.
  4. Annotate your test methods with the org.junit.Test annotation.
  5. The handles to com.sun.jersey.api.client.Client and com.sun.jersey.api.client.WebResource instances - client and webResource get inherited from the JerseyTest class. You can use them in your test methods for building URIs and sending HTTP requests.
  6. Run the tests using the maven command - mvn clean test. This will by default run the tests against the Grizzly Web Server. If you want to run the tests on the container of your choice, set the system property container.type with one of the following values:
    • EmbeddedGF : Makes the tests run against Embedded GlassFish.
    • GrizzlyWeb : Makes the tests run against the Grizzly Web container.
    • HTTPServer : Makes the tests run against the Simple HTTP Server.
  7. The framework also provides an option of seeing the HTTP requests and responses sent over the wire. It could be done by just setting the system property enableLogging, i.e., if you want to see the request and response sent over the wire, while running tests against Embedded GlassFish, execute the following command:
    • mvn clean test -Dcontainer.type=EmbeddedGF -DenableLogging
  8. And that's it. You have got the framework working.

Are there any samples which are using this framework?

                 Some of the samples that come with the Jersey distribution have been modified to use this framework. These are:

You should try running tests of these samples and see how the test framework works. I'm sure you will like it :)

If you see some of these samples do not have a deployment descriptor, but still you are able to run the tests against Embedded GlassFish. This is because the framework generates a deployment descriptor on the fly in such cases.

Future Enhancements

It is being planned to support the following features in the coming versions:

  1. Support for external containers - GlassFish v2 and GlassFish v3
  2. Giving the user an option to specify the containers which his test doesn't support.

If you have any queries or see any issues with the current implementation or feel there should be something more, please send an email to the Jersey user mailing list - users@jersey.dev.java.net

Comments:

Nice article. I wanted to try the test framework. However, I was not able to locate the JAR that contains test framework classes.

Posted by Surajit Pal on June 17, 2009 at 07:42 AM PDT #

Surajit,
unfortunately the framework is currently available only as a maven module on the maven repository.
Could you please try it in a maven environment? An alternate solution would be to download the framework jar files from [1]. Also, you will have to download some other jar files like the jersey-client, jersey-server, etc.

[1] http://download.java.net/maven/2/com/sun/jersey/test/framework/jersey-test-framework/1.1.0-ea/jersey-test-framework-1.1.0-ea.jar

Please let me know if you need more info.

Posted by Naresh on June 28, 2009 at 09:04 PM PDT #

If your application uses the Spring framework, currently you have to pass the spring context loader listener class and the applicationContext config location to the test framework explicitly.

Your test class constructor should look as follows:
===================================================================
public YouAppTest() throws Exception {
super();
Map<String, String> contextParams = new HashMap<String, String>();
contextParams.put("contextConfigLocation", "classpath:applicationContext.xml");
ApplicationDescriptor appDescriptor = new ApplicationDescriptor()
.setContextPath(your-context-path)
.setRootResourcePackageName(your-root-resource-package-name)
.setServletClass(com.sun.jersey.spi.spring.container.servlet.SpringServlet.class)
.setContextListenerClassName("org.springframework.web.context.ContextLoaderListener")
.setContextParams(contextParams);
super.setupTestEnvironment(appDescriptor);
}
===================================================================

Also, you might want to refer the spring-annotations sample [1] to see how the JerseyTest class has been used in a spring app.

[1] http://download.java.net/maven/2/com/sun/jersey/samples/spring-annotations/1.1.1-ea/

Posted by Naresh on July 14, 2009 at 08:34 PM PDT #

One of the users Tarjei has suggested [1] the following so that the download of the GlassFish dependencies could be avoided if the test doesn't really require EmbeddedGlassFish:
------------------------------------------
you can also exclude the 18M glassfish dependency and still run the tests.

<dependency>
<groupId>com.sun.jersey.test.framework</groupId>
<artifactId>jersey-test-framework</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.glassfish.embedded</groupId>
<artifactId>glassfish-embedded-all</artifactId>
</exclusion>
</exclusions>
</dependency>
------------------------------------------

[1] http://markmail.org/search/?q=JerseyTest#query:JerseyTest%20from%3A%22tarjei%22+page:1+mid:jdpiitcm3ev5u3sa+state:results

Posted by Naresh on July 14, 2009 at 11:09 PM PDT #

https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html#d4e1302 has up to date infos about the Jersey test framework

Posted by Renaud on January 21, 2010 at 06:41 PM PST #

I was able to create Jersey Test Classes and run it against the Grizzly server in memory. How can a Jersey/JUnit test suite be created using these Jersey Test Classes?

Posted by Arun on April 26, 2010 at 03:04 PM PDT #

I was able to create Jersey Test Classes and run it against the Grizzly server in memory. How can a Jersey/JUnit test suite be created using these Jersey Test Classes?

Posted by Arun on April 26, 2010 at 03:05 PM PDT #

Arun,

a Jersey test class is no different from a JUnit test class. You can create your test suite the same way as you do for the regular JUnit tests.

Posted by Naresh on April 26, 2010 at 04:21 PM PDT #

Naresh,

I don't understand how you can call the JUnit assert methods (or create test suites) in classes that extend JerseyTest. A JerseyTest is not a JUnit test.

The helloworld example doesn't compile.

Posted by Neil on April 29, 2010 at 12:36 AM PDT #

Nell,

the class JerseyTest is actually a JUnit 4.x test, and hence any class which extends JerseyTest would implicitly inherit all the JUnit methods.

Please have a look at the Jersey helloworld sample [1]. This sample has a test class which extends JerseyTest. In this example you can see, the JUnit 4.x annotation @Test, and the assert methods being used.

[1] http://download.java.net/maven/2/com/sun/jersey/samples/helloworld/1.1.5.1/helloworld-1.1.5.1-project.zip

Posted by Naresh on April 29, 2010 at 02:52 AM PDT #

Has anyone seen this error before when junit testing jersey with spring:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ACResource': Scope 'request' is not active for the current thread; ....

My Resource is defined with @Scope("request")
It all works perfectly when deployed. But when unit testing using grizzly, it fails.

Any ideas ?

Posted by phil on May 18, 2010 at 01:07 AM PDT #

Phil,
which version of the Jersey Test Framework are you using? I'm hoping you are trying to test with the GrizzlyWebContainer and not the GrizzlyContainer.

Please have a look at the Jersey spring-annotations sample [1], which works with the Grizzly Web Container.

In case you have any troubles, please feel free to send a mail to the Jersey users list users@jersey.com, which is pretty active.

[1]http://download.java.net/maven/2/com/sun/jersey/samples/spring-annotations/1.2/spring-annotations-1.2-project.zip

Posted by Naresh on May 18, 2010 at 01:19 AM PDT #

I'm sorry, I mistyped the user alias. It is users@jersey.dev.java.net.

Posted by Naresh on May 18, 2010 at 01:21 AM PDT #

Hi,
Can I use jersey for test response from my spring @Contrlollers?

Posted by Viktor on September 27, 2010 at 03:44 AM PDT #

Viktor,

sure you can do that.

Please refer the spring-annotations sample -

http://download.java.net/maven/2/com/sun/jersey/samples/spring-annotations/1.4-ea06/spring-annotations-1.4-ea06-project.zip

Posted by Naresh on September 27, 2010 at 01:23 PM PDT #

All the examples (samples) have just one Test class it. How can I have multiple Test classes and run all of the tests.

I tried having more than one Test classes, but found that only one Test class runs

Posted by Raj on October 07, 2010 at 08:11 AM PDT #

Raj,

I was able to reproduce the problem you mentioned.
Looks like this happens when the test class name doesn't end with the suffix "Test".

For instance, in the helloworld-webapp sample, I copied the test class HelloWorldWebappTest.java to HelloWorldWebappTest2.java, and then did a "mvn clean test". This test run did not consider the the other test class "HelloWorldWebappTest2". However, when I refactored that class to HelloWorld2WebappTest.java, "mvn clean test" did run the tests from both the classes.

Probably, this is an issue with Maven's way of finding tests.

HTH.

Posted by Naresh on October 07, 2010 at 03:20 PM PDT #

Hi

Posted by guest on November 12, 2010 at 03:57 PM PST #

Could someone point me towards an example that does not use Maven?

Posted by Jared on November 22, 2010 at 02:35 AM PST #

do you know how to run this unit test inside eclipse?

Posted by ely on December 21, 2010 at 11:46 AM PST #

ely,

probably you should try installing some maven plugin for eclipse, like - m2eclipse, and try running the tests using that plugin.

Posted by Naresh on December 21, 2010 at 12:40 PM PST #

how to integrate jesey test framework with mockito or easymock testing frameworks.

Posted by raj on January 20, 2011 at 07:41 AM PST #

Raj,

this was discussed recently in the user forum.
Please have a look at http<colon><double slash>jersey.576304.n2.nabble.com<slash>How-to-Use-Mocks-With-Jersey-Test-Framework-td4461311<dot>html

Posted by Naresh on January 20, 2011 at 04:43 PM PST #

fsdfdsf

Posted by dsadas on January 25, 2011 at 02:09 AM PST #

I agree with this article. IT can be easy with the use of framework. It is very useful in these days.

Posted by DB2 SQL on March 02, 2011 at 12:52 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Naresh worked at Sun Microsystems for two years. During these two years he had worked on the Project Metro and Project Jersey.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today