On Jun 23, 2008, at 2:21 PM, Martin Probst wrote:
> IMHO i don't really see much difference between the two from the
> perspective of the resource class. Jersey runs such unit tests
> without any issue.
>
> Well, there is quite a difference in the amount of code you have to
> write for the unit test itself, isn't it? Instead of simply e.g.
> calling a method to test it, you'll have to wire up all the routes
> and go through HTTP. Plus you might have to expose functionality
> that should be internal.
>
I managed to reduce it by the unit test extending an abstract class
that did all the boiler plate. But i agree that one has to use
something else to invoke the resource class, which is why i developed
a client API that reduces client-side code. Since you are writing
code to the HTTP interface it should not expose anything internal if
youare testing that interface using a HTTP client API.
See end of email for some example test code when using Grizzly. I
reckon this could be improved quite a bit.
In your example you had:
@Context UriInfo ui;
This class only makes sense when there is a HTTP request of some form
so that the UriInfo instance can obtain the URI information sent by
the client.
Paul.
public class HttpMethodTest extends AbstractGrizzlyServerTester {
@Path("/test")
public static class HttpMethodResource {
@GET
public String get() {
return "GET";
}
@POST
public String post(String entity) {
return entity;
}
@PUT
public String put(String entity) {
return entity;
}
@DELETE
public String delete() {
return "DELETE";
}
}
public HttpMethodTest(String testName) {
super(testName);
}
public void testGet() {
startServer(HttpMethodResource.class);
WebResource r = Client.create().resource(getUri().path
("test").build());
assertEquals("GET", r.get(String.class));
}
public void testPost() {
startServer(HttpMethodResource.class);
WebResource r = Client.create().resource(getUri().path
("test").build());
assertEquals("POST", r.post(String.class, "POST"));
}
public void testPut() {
startServer(HttpMethodResource.class);
WebResource r = Client.create().resource(getUri().path
("test").build());
assertEquals("PUT", r.post(String.class, "PUT"));
}
public void testDelete() {
startServer(HttpMethodResource.class);
WebResource r = Client.create().resource(getUri().path
("test").build());
assertEquals("DELETE", r.delete(String.class));
}
public void testAll() {
startServer(HttpMethodResource.class);
WebResource r = Client.create().resource(getUri().path
("test").build());
assertEquals("GET", r.get(String.class));
r = Client.create().resource(getUri().path("test").build());
assertEquals("POST", r.post(String.class, "POST"));
r = Client.create().resource(getUri().path("test").build());
assertEquals("PUT", r.post(String.class, "PUT"));
r = Client.create().resource(getUri().path("test").build());
assertEquals("DELETE", r.delete(String.class));
}
}
Paul.
> I'll see if I can somehow make it work using a custom
> ComponentProvider.
>