Discussion:
JerseyTest with a resource containing @Inject
(too old to reply)
Matyas Bene
2014-10-23 13:10:13 UTC
Permalink
After much trial and error while trying to test one of my JAX-RS resource using the JerseyTest framework (to be precise, I am extending JerseyTestNg.ContainerPerClassTest), I narrowed down my problem to the @Inject annotation. That is, whenever my resource contains at least 1 such field annotated with @Inject, any call to that resource inside the test framework fails with HTTP-500, even though that field is never used. Running the same resource in Glassfish certainly works as expected.



What would be the recommended way of testing such resources? Using jersey 2.9 (as shipped with Glassfish)



I also tried creating a ServletDeploymentContext, but in that, however, I received countless warnings from HK2, that injection points can not be satisfied.



Jersey 2.13 was a tad better. It did not crash with HTTP-500, but printed more meaningful exceptions (HK2 complaining about no object being available for injection) even without using ServletDeploymentContext. However, my test could not be run. Not sure how to tell HK2 that I either: don’t need those fields, or where to find the for injection?



Regards,

Matyas
Matyas Bene
2014-10-23 14:16:06 UTC
Permalink
Update: got it working.

While I was trying hard creating an AbstractBinder to satisfy HK2, i registered the binder using the class, i.e. register(MyBinder.class);. Turns out it does not work.

Instead I had to register(new MyBinder()); which did the trick. All fine now.



From: Matyas Bene [mailto:matyas.bene-mY8PkvhUYG9Wk0Htik3J/***@public.gmane.org]
Sent: Thursday, October 23, 2014 3:10 PM
To: 'users-ywjJWXFEILO43ww/***@public.gmane.org'
Subject: JerseyTest with a resource containing @Inject





After much trial and error while trying to test one of my JAX-RS resource using the JerseyTest framework (to be precise, I am extending JerseyTestNg.ContainerPerClassTest), I narrowed down my problem to the @Inject annotation. That is, whenever my resource contains at least 1 such field annotated with @Inject, any call to that resource inside the test framework fails with HTTP-500, even though that field is never used. Running the same resource in Glassfish certainly works as expected.



What would be the recommended way of testing such resources? Using jersey 2.9 (as shipped with Glassfish)



I also tried creating a ServletDeploymentContext, but in that, however, I received countless warnings from HK2, that injection points can not be satisfied.



Jersey 2.13 was a tad better. It did not crash with HTTP-500, but printed more meaningful exceptions (HK2 complaining about no object being available for injection) even without using ServletDeploymentContext. However, my test could not be run. Not sure how to tell HK2 that I either: don’t need those fields, or where to find the for injection?



Regards,

Matyas
Stefan Bodewig
2014-10-23 16:09:38 UTC
Permalink
Post by Matyas Bene
After much trial and error while trying to test one of my JAX-RS
resource using the JerseyTest framework (to be precise, I am extending
JerseyTestNg.ContainerPerClassTest), I narrowed down my problem to the
@Inject annotation. That is, whenever my resource contains at least 1
the test framework fails with HTTP-500, even though that field is
never used. Running the same resource in Glassfish certainly works as
expected.
I'm using JUnit and Jersey 2.13 but that shouldn't matter too much.

You must make sure the application you return in your test's configure
method contains a valid HK2 binding for the injectee. You will want to
register an AbstractBinder and bind your collaborators there.


Something like

@Override
protected Application configure() {
return new ResourceConfig(Resource.class)
.register(new AbstractBinder() {
.register( new AbstractBinder() {
@Override
protected void configure() {
bind(SOME_INSTANCE).to(YOUR_INJECTEE.class);
}
});
}

or using class based or factory based bindings or whatever suits you
best. I tend to bind mock objects here.

Stefan

Loading...