Download Jersey 1.9-ea01 User Guide
Transcript
Jersey Test Framework
public class MainTest extends JerseyTest {
public MainTest()throws Exception {
super("com.sun.jersey.samples.helloworld.resources");
}
@Test
public void testHelloWorld() {
WebResource webResource = resource();
String responseMsg = webResource.path("helloworld").get(String.class);
assertEquals("Hello World", responseMsg);
}
}
Note super call in constructor - it passes list of package names to scan (it really is a list, JerseyTest
constructor has variable argument count). Another useful method is resource() which returns
WebResource instance with URI set to base URI of your application. You can get preconfigured Jersey
Client instance similarly by calling client() method.
7.4. Creating own module
Creating your own module is pretty straightforward, you just have to implement
com.sun.jersey.test.framework.spi.container.TestContainerFactory
and
com.sun.jersey.test.framework.spi.container.TestContainer.
TestContainer
factory is there basically for returning TestContainer instance and TestContainer has self-explanatory
methods: start(), stop(), getClient() and getBaseURI(). I recommend taking look at source
code and read javadoc of these two classes, all you need is there.
You should be avare of another thing when implementing own jersey test framework module. If you want it
to be usable by running just mvn clean install (when only your module is specified), you need to add
META-INF/services/com.sun.jersey.test.framework.spi.container.TestContainerFactory file into your jar
and put there your factory class (fully classified) name.
7.5. Running tests outside Maven
Since Jersey is Maven based project, executing tests without Maven can be painful. You have to have
everything needed present on classpath and by everything is meant following list:
• jersey-server
[http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.9-ea01/jerseyserver-1.9-ea01.jar]
• jersey-core
ea01.jar]
[http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.9-ea01/jersey-core-1.9-
• jsr311-api [http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.1/jsr311-api-1.1.jar]
• asm [http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar]
• jersey-test-framework-grizzly
[http://download.java.net/maven/2/com/sun/jersey/jersey-testframework/jersey-test-framework-grizzly/1.9-ea01/jersey-test-framework-grizzly-1.9-ea01.jar]
• jersey-test-framework-core [http://download.java.net/maven/2/com/sun/jersey/jersey-test-framework/
jersey-test-framework-core/1.9-ea01/jersey-test-framework-core-1.9-ea01.jar]
55