users@jersey.java.net

Re: [Jersey] Unit tests and UriInfo

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 27 Oct 2008 17:41:45 +0100

On Oct 27, 2008, at 3:54 PM, Gili wrote:

>
> Hi Andy,
>
> I've been doing the same (using Mockito). The only bit I am
> questioning is
> applicationsResource.setUriInfo(). I am reluctant to expose any
> test-specific functions in the applicationsResource. In an ideal world
> UriInfo would be passed in the constructor along with the other
> objects
> being injected. Unfortunately Guice knows how to inject one half of
> the
> objects, Jersey the other, and I don't think they can (currently)
> interoperate on this.
>
> For now I am looking into getting Guice to inject UriInfo as null in
> production (both into the constructor) and as a mock during testing.
> At
> least then I'm not exposing any testing-specific methods and when
> Guice/Jersey learn to interoperate in the future this will work in
> the same
> fashion except that then UriInfo won't be null for production.
>

Hmm.... i am wondering if is possible to do the following when mocking
up (using 1.0.1-SNAPSHOT):

UriInfo uriInfo = mock(UriInfo.class);
uriBuilder = mock(UriBuilder.class);
stub(uriInfo.getAbsolutePathBuilder()).toReturn(uriBuilder);
stub(uriBuilder.path(APP_ID)).toReturn(uriBuilder);
stub(uriBuilder.build()).toReturn(new URI(URI));
applicationsResource.setUriInfo(uriInfo);

InjectableProviderFactory injectableFactory = new
InjectableProviderFactory();
injectableFactory.add(new ContextInjectableProvider<UriInfo>(
    UriInfo.class, uriInfo));
Object resource = ...
Class c = resource.getClass();
ComponentInjector i = new ComponentInjector(
                     injectableFactory,
                     c
                     );
i.inject(resource);

It is a lot of work to set up but i suppose common helper
functionality could be created.


Alternatively in Guice is there functionality to add a general post
create listener to some Guice context that can set the field on the
instance of the specific class?

Paul.