On Jun 23, 2008, at 11:20 AM, Martin Probst wrote:
> Hi all,
>
> I have a question about unit testing of resource classes. I have
> trouble setting up injection so that I can actually use the
> classes. E.g.:
>
> @Path("...")
> class FooResource {
> @Context UriInfo info;
> }
>
> Now I want to unit test this method - but how do I get the fields
> injected in my test harness?
>
I just realized i answered your question incorrectly. You can use a
different container, for example Grizzly, at the end of the email
shows a standalone example.
You can also look at the Jersey unit tests, see packages
com.sun.jersey.impl.container.grizzly // test embedded Grizzly
com.sun.jersey.impl.container.grizzly.web // test embedded Servlet
com.sun.jersey.impl.container.httpserver // test embedded LW HTTP
server
It is also possible to use Glassfish in embedded mode. The Jersey
spring unit tests are using Jetty.
Paul.
import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.grizzly.GrizzlyServerFactory;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
/**
*
* @author paulsandoz
*/
public class Main {
@Path("/")
public static class Resource {
@Context UriInfo ui;
@GET
public String get() { return ui.getAbsolutePath
().toASCIIString(); }
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
SelectorThread st = GrizzlyServerFactory.create("http://
localhost:9999/");
try {
Client c = Client.create();
WebResource r = c.resource("
http://localhost:9999/");
String s = r.get(String.class);
System.out.println(s);
} catch (UniformInterfaceException e) {
System.out.println(e.getResponse().getStatus());
e.printStackTrace();
} finally {
st.stopEndpoint();
System.exit(0);
}
// TODO code application logic here
}
}