It was not easy, but I found out. The thing is, in my JUnit test, I created the server as follows:
HttpServer server = HttpServerFactory.create(url);
But in this case, you create a lightweight container in which there are no servlet containers, and the reason for the failure. Therefore, to get all this, I used a jersey-test framework that allows you to use the Grizzly web container (or even the built-in glass fish).
Here is the maven:
<dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey.test.framework</groupId> <artifactId>jersey-test-framework</artifactId> <version>1.0.3</version> <scope>test</scope> </dependency> </dependencies>
Here is an example of a JerseyServerTest: note that it extends the JerseyTest
public class JerseyServerTest extends JerseyTest { protected String baseUri = "http://localhost:" + TestConstants.JERSEY_HTTP_PORT + "/"; public JerseyServerTest() throws Exception { super("com.robustaweb.library.rest.controller"); } @Test public void testHelloWorldRequest() { SunRestClient client = new SunRestClient(baseUri + "root"); String result = client.GET("", null); System.out.println(result); } @Test public void testDeleteRequest() { SunRestClient client = new SunRestClient(baseUri + "root"); String result = client.DELETE("john", null); System.out.println(result); } }
And finally, a resource file containing @GET and @DELETE
@Path("root") public class JaxRsController extends JaxRsResourceController{ List<String> peoples = new ArrayList<String>(); @GET public String hello(){ System.out.println("Uri is "+getUri()); return "Hello "+peoples; } @DELETE @Path("{name}") public String deletePeople(@PathParam("name") String name){ System.out.println("deleting "+name); this.peoples.remove(name); return String.valueOf(peoples.size()); } }
And now it works! I got some help in this article , and there is a small chapter in the documentation . Beeing, capable of attaching the source code for the Jersey framework, really helped, so he also wants IntelliJ.
source share