Hi Marko,
please see my comments inline.
Mira
On 7.5.2013 21:39, Marko Asplund wrote:
> Hi Mira,
>
> After clearing my Maven settings and local repository I finally got
> Jersey to build on my machine. The build on BuildHive also seems to be
> working now.
>
> I came across a small problem when looking into porting the old test code base.
> Does the Jersey test framework support registering custom beans to be
> injected with @Inject?
> When I try to get my custom POJO service injected using @Inject the
> lookup fails, but if I register the POJO with HK2 ServiceLocator, it
> gets looked up correctly.
What exactly you mean by a "custom bean"? Where would you like to have
it injected - server code, your test? Maybe small example would help.
Currently, you cannot inject anything into the JerseyTest code. Means
that your unit test will use any injection. The server code you are
using in the JerseyTest can use injections. Which injections you can use
depends on the deployment schema. By default the tests are run on
Grizzly container but not as servlets. Therefore it does not support
CDI. In order to support CDI you would need to deploy the application to
Glassfish and use external cotnainer factory in the jersey test. You can
do it by overriding this method in the JerseyTest:
@Override
protected TestContainerFactory getTestContainerFactory() throws
TestContainerException {
return new ExternalTestContainerFactory();
}
Using external contianer factory causes that JerseyTest will not deploy
and start the application for you but you need to start the server side
of your test by yourself.
Other injections can be used using HK2 service locator. You can register
your custom binders that will registers your binding. This is simple
example of feature that registers a custom binding:
@Override
public boolean configure(FeatureContext context) {
context.register(new AbstractBinder() {
@Override
protected void configure() {
bind(YourCustomImplementation.class).to(YourCustomInterface.class).in(Singleton.class);
}
});
return true;
}
Such a feature you will register into the ResourceConfig in the jersey
test by overriding the configure() method from JerseyTest.
Or should I use a CDI implementation for this? Does Jersey test
framework work with third party CDI implementations?
We currently support RI of CDI (weld).
>
> thanks,
>
> marko