views:

1765

answers:

10

I have a JAX-RS web service implemented with Jersey library and now I want to test it. In order to do that I'd like to host this service in my test by preinitializing it with mocked services.

What is the best way to host such a service and execute the test calls?

@Path("/srv")
public class MyService
{
   @GET
   public void action(@Context UriInfo uri)
   { ... }
}

@Test
public void myTest()
{
   MyService service = new MyService();
   service.setSomething(...);

   // How do I host it?

   // How do I call it?
}
+2  A: 

I haven't tried it, but a JUnit extension like HtmlUnit or HttpUnit may be a good way to test a JAX-RS/Jersey service. The test case can use XPaths to find expected return values and validate the returned value against the expected. See: http://htmlunit.sourceforge.net/gettingStarted.html for more info.

Jack Cox
+1  A: 

You can use Grizzly to host the services and then use the Jersey Client to access them. Take a look at the sample applications. For example, in the Bookstore sample you may find the TestSupport class and JerseyTest class (found in the jersey-test-framework) of particular interest.

I hope this helps.

(Unfortunately Stack Overflow wouldn't let me post until I removed all the hyperlinks so happy Googling!).

Jonathan
A: 

Have you looked in to using the Jersey Test Framework? Unfortunately it's still more integration test than unit test, but it might get you on your way.

safetydan
Yep, I've seen it. This is THE problem that it's an integration tests. To avoid the integration tests, I should be able to create a real instance of the service class, initialize it with mocks and then pass it to Jersey to host it IN PROCESS.
IgorM
+4  A: 

I believe the Jersey Test Framework provides a solution for your requirement. It allows you to deploy a single service, and run all its tests. You could use the framework to run your tests against Grizzly Web Container, Embedded GlassFish and/or HTTPServer.

Please note that you could use the framework to run your tests against the regular web containers like GlassFish and Tomcat too. In case you have any more queries please feel free to send me or the Jersey users mailing list - [email protected] an e-mail.

This framework does not allow to initialize an instance of the service in the same place as the test definition. It still requires to start the whole server via the config file.
IgorM
A: 

Okay I get it now. Right now the framework doesn't support IN PROCESS, bt we are working on it. We will see that this support would be added in a coming version of the Jersey Test Framework

Thanks for the response. When do you target to have it? This is very important part of the testing process - the "in process" allows developers to properly initialize the services/components and mock the related parts to isolate them from the rest of the system and enable the assertion of expectations. There is no way around it.
IgorM
I cannot give a dateline right now, but will see that this support is added asap. Will keep you posted once it is ready.
A: 

Anyone knows if Jersey Test Framework works with Spring Security? I ve been trying but its impossible to configure the Spring filter DelegatingFilterProxy with the current api (1.1.0-ea)

qtxo
A: 

Hi,

Please let me on how to use Jersey Test Framework with Tomcat. I have a call to super.setupTestEnvironment(appDescriptor); It throws java.lang.NoClassDefFoundError: com/sun/grizzly/http/embed/GrizzlyWebServer

Thanks, Sakuntala.

+6  A: 
A: 

@Sakuntala:

I have tried running a couple of tests on Tomcat using the Jersey Test Framework, and have been successful. I hope you have set the container.type property to External since you are running you tests on an external container. Have you been able to resolve the mentioned problem? If you still face this problem can you try the revised version of the Jersey Test Framework (1.1.2-ea). Please note that there are some API changes in this latest version. Please refer the blog entry http://blogs.sun.com/naresh/entry/jersey_test_framework_re_visited for detailed description of what has changed.

Please send an e-mail to the Jersey users mailing list [email protected], in case you have any problems. The mailing list is very active and you might get a quicker response.

A: 

How to create TESTSuite in Jersey Test Framework. When we add TestCases to JUnit TestSuite it gives some intializaton and class cast exception. so you can't build testsuite to execute bunch of test cases at once.

Bhaveshkumar Thaker