users@jersey.java.net

[Jersey] Re: JerseyTest and Guice

From: Gili <cowwoc_at_bbs.darktech.org>
Date: Sun, 20 Mar 2011 10:56:01 -0700 (PDT)

Gili wrote:
>
> I'm a bit frustrated. I've been trying for days to get Guice to work with
> any jersey-test container. I couldn't find a way to register GuiceFilter
> with InMemoryTestContainer. I registered GuiceFilter and
> GuiceServletContextListener with GrizzlyTestContainer but it still tried
> using Jersey for IoC. I tried the same with EmbeddedGlassfish and got "The
> ResourceConfig instance does not contain any root resource classes".
>
> Can someone please point me to a complete/running testcase that
> illustrates how to get jersey-test working with Guice?
>
> Thank you,
> Gili
>

I found
http://download.java.net/maven/2/com/sun/jersey/samples/guicefilter-webapp/1.5/.
Using WebAppDescriptor.Builder().filterClass() instead of addFilter() seems
to fix GrizzlyTestContainer not using Guice for IoC (can anyone explain
why?) but I am still getting an error "The ResourceConfig instance does not
contain any root resource classes".

My code works as follows. The class that extends JerseyTest runs:

                super(new
WebAppDescriptor.Builder().contextListenerClass(ServletConfiguration.class).
                        filterClass(GuiceFilter.class).build());

ServletConfiguration configures the Guice Injector as follows:

                                Map&lt;String, String&gt; jerseyParams = Maps.newHashMap();
                                jerseyParams.put(ServletContainer.RESOURCE_CONFIG_CLASS,
                                        ClasspathResourceConfig.class.getName());
                                jerseyParams.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
                                serve("/*").with(GuiceContainer.class, jerseyParams);

As such, I am expecting Jersey to find the root resources off the classpath.
This works fine for production but fails in the test code. I tried passing a
list of packages to the JerseyTest constructor but this did not work either.

It looks like Jersey's classpath scanner is looking in the wrong place. Here
is what I see when running unit tests:

INFO: Scanning for root resource and provider classes in the paths:
  C:\Users\Gili\Documents\foo\WEB-INF\lib
  C:\Users\Gili\Documents\foo\WEB-INF\classes

Here is what I see when running the project:

INFO: Scanning for root resource and provider classes in the paths:
  C:\Users\Gili\Documents\foo\target\com.foo\WEB-INF\lib
  C:\Users\Gili\Documents\foo\target\com.foo\WEB-INF\classes

The paths referenced by the unit test do not exist (notice even the "target"
directory is missing). Where is it getting those paths from?

Thanks,
Gili

--
View this message in context: http://jersey.576304.n2.nabble.com/Re-Re-Jersey-JerseyTest-and-Guice-tp5675712p6190053.html
Sent from the Jersey mailing list archive at Nabble.com.