CDI-Unit for testing classes using @Resource and @Context annotations?

927 views
Skip to first unread message

LF

unread,
Jul 16, 2013, 2:14:02 AM7/16/13
to cdi-...@googlegroups.com
Hi

We have successfully used CDI-Unit to test our CDI-based JAX-RS solution (however normally running on WebSphere using the bundled Wink and Apache OpenWebBeans)...

However in the JAX-RS solution we also have some cases when we need to handle classes that also contain @Resource and @Context (e.g. JAX-RS UriInfo or HttpHeaders) annotations....

Some of these classes are CDI managed beans (e.g. the JAX-RS resource classes) and some are not.

Is there a way with CDI-Unit or combination of CDI-Unit and some other tool Unit test these classes and provide mock objects for the fields annotated with e.g. @Context ?

Regards
LF

Bryn Cooke

unread,
Jul 16, 2013, 4:23:37 AM7/16/13
to cdi-...@googlegroups.com
Hi,

As you have guessed, the @Context injections are performed by your JAX-RS container and therefore CDI won't inject them. I can think of a couple of things you could look in to:
  • Create a custom extension that enables CDI to inject @Context fields. Possibly in the ProcessAnnotatedType phase you can introduce the inject annotation on to fields that have @Context. You're looking to create new injection points...
  • Depending on how you are testing your web services moving the you may be able to shift @Context fields on to parameters of your methods. If you are calling the method directly from your unit test then you can just supply a mock object as a parameter.
Hope this helps.

Bryn

LF

unread,
Jul 16, 2013, 5:28:10 AM7/16/13
to cdi-...@googlegroups.com
Thanks for the quick response....

Some of the classes could maybe be refactored so that we can manually set mocked objects as the fields/parameters.

However I would be very interested if you had some example or webpage or similar that shows (or give hints about) how to make a custom CDI extension in the way you describe.

Regards
LF

Bryn Cooke

unread,
Jul 16, 2013, 5:40:04 AM7/16/13
to cdi-...@googlegroups.com
I'm afraid all I can do is point you towards the weld docs:

http://docs.jboss.org/weld/reference/latest/en-US/html/extend.html#d0e4772

I would start looking at the processAnnotatedType phase. Make sure you are using deltaspike or seam solder to create a mutable annotated type and then add the @Inject wherever you find @Context. That way CDI will be able to inject the in to @Context fields as normal.

Bryn

LF

unread,
Jul 18, 2013, 3:40:51 AM7/18/13
to cdi-...@googlegroups.com
Hi again

I tried to create a test cdi extension, a class implementing the javax.enterprise.inject.spi.Extension interface... I then added the fully qualified class name to a filed called javax.enterprise.inject.spi.Extension in META-INF/services

When I tried to run a CDI-Unit testcase none of the phases I observed triggered... then I tried to debug and see if it even tried to instantiate the class itself but it did not...

Can you give me a hint on how I can it working so that the Extension class kicks in...?

Regards
LF

LF

unread,
Jul 18, 2013, 3:49:00 AM7/18/13
to cdi-...@googlegroups.com
Hi again

Answerering my own question... I was to quick to post it here... after looking at the CDI-Unit User guide I realize that I needed to add the extesion class to the @AdditionalClasses annotation to get picked up....

Regards
LF

LF

unread,
Jul 19, 2013, 7:35:33 AM7/19/13
to cdi-...@googlegroups.com
Hi again

I did the following:

- Created a CDI Extension with a method to observe ProcessAnnotatedType events....
- I created wrapper classes for Annotation, AnnotatedType, AnnotatedMethod etc
- The extension has a configurable annotation replacement so I can tell it to e.g. replace @Context for @Inject and so on...

=> Result is that I now can use CDI to inject also @Resource and @Context annotated classes and unit test them in the same way as CDI managed beans....

Thanks for pointing me in the right direction....

Regards
LF

Bryn Cooke

unread,
Jul 19, 2013, 9:04:24 AM7/19/13
to cdi-...@googlegroups.com
No Problem.
Do you think this could be a generic feature that we could use in CDI unit? Perhaps using something like @InjectAnnotations(Context.class) or @ReplaceAnnotation(Resource.class, Inject.class) on the unit test?

Cheers,

Bryn


--
You received this message because you are subscribed to the Google Groups "CDI-Unit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tomasz Bartczak

unread,
Feb 19, 2014, 6:02:25 AM2/19/14
to cdi-...@googlegroups.com
Hi,

We are trying to set up cdi-unit to test JaxRS application, but we failed to do this.
Could you help us a bit with that?
My skeleton solution that is deployable to glassfish and CDI Injection works there, but not in cdiunit tests is here:
https://github.com/kretes/cdiunit-jaxrs-poc

We would be grateful for some help.
After we find out the solution we will publish it anyway for future reference

Bryn Cooke

unread,
Feb 19, 2014, 8:06:05 AM2/19/14
to cdi-...@googlegroups.com

I am happy to help, but am currently on holiday. Back next week.
Bryn

Tomasz Bartczak

unread,
Mar 8, 2014, 7:30:58 AM3/8/14
to cdi-...@googlegroups.com
We have worked the problem around.

From the cdi-unit side the biggest problems was:
 - cdi context was kept in thread-local, and we needed some broader scope to use the same context in jax-rs server.
 - to overcome this we needed to introduce a class seam (add cdiunitcontextfactory in the same package in our test sources) as there we couldn't find a way to specify that.

I don't know what the case will be in future version of cdi-unit, but we are bound to old weld version, so we cannot update cdi-unit that much.

bryn

unread,
Mar 10, 2014, 10:54:58 AM3/10/14
to cdi-...@googlegroups.com
The root of many of the problems that we are having is that CDI-Unit only includes the bare skeleton of the HTTP* classes to inject. However it turns out that these are all interlinked e.g. you can get a session from a request. I think more comprehensive stubs for the HTTPS classes are developed.

Bryn
For more options, visit https://groups.google.com/d/optout.

Carlos Silva

unread,
Jul 16, 2015, 4:47:33 PM7/16/15
to cdi-...@googlegroups.com
Following up on this, do you know if this functionality ever made it into CDI unit or perhaps some other library? We are in need  of the same thing where we want to inject @Resource.

Thanks,
Carlos 

Bryn Cooke

unread,
Jul 16, 2015, 7:08:35 PM7/16/15
to cdi-...@googlegroups.com, car...@casadesilva.com
No this was never added. If you have control over the code then the maybe just add @Inject in addition to @Resource.

Bryn 

Luís Soares

unread,
Nov 10, 2015, 6:17:06 AM11/10/15
to CDI-Unit
Hello LF,

Could you share the code?
Thanks
Reply all
Reply to author
Forward
0 new messages