Download Jersey 2.1 User Guide
Transcript
Chapter 18. Jersey Test Framework
Jersey Test Framework originated as an internal tool used for verifying the correct implementation of
server-side components. Testing RESTful applications became a more pressing issue with "modern"
approaches like test-driven development and users started to look for a tool that could help with designing
and running the tests as fast as possible but with many options related to test execution environment.
Current implementation of Jersey Test Framework supports the following set of features:
• pre-configured client to access deployed application
• support for multiple containers - grizzly, in-memory, jdk, simple
• able to run against any external container
• automated configurable traffic logging
Jersey Test Framework is based on JUnit and works almost out-of-the box. It is easy to integrate it within
your Maven-based project. While it is usable on all environments where you can run JUnit, we support
primarily the Maven-based setups.
18.1. Basics
1 public class SimpleTest extends JerseyTest {
2
3
@Path("hello")
4
public static class HelloResource {
5
@GET
6
public String getHello() {
7
return "Hello World!";
8
}
9
}
10
11
@Override
12
protected Application configure() {
13
return new ResourceConfig(HelloResource.class);
14
}
15
16
@Test
17
public void test() {
18
final String hello = target("hello").request().get(String.class);
19
assertEquals("Hello World!", hello);
20
}
21 }
If you want to develop a test using Jersey Test Framework, you need to subclass
JerseyTest [http://jersey.java.net/nonav/apidocs/snapshot/jersey/org/glassfish/jersey/org/glassfish/jersey/
test/JerseyTest.html] and configure the set of resources and/or providers that will be deployed as part of
the test application. This short code snippet shows basic resource class HelloResource used in tests
defined as part of the SimpleTest class. The overridden configure method returns a ResourceConfig
[http://jersey.java.net/nonav/apidocs/snapshot/jersey/org/glassfish/jersey/server/ResourceConfig.html] of
the test application,that contains only the HelloResource resource class. ResourceConfig
is a sub-class of JAX-RS Application [http://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/
181