Over a million developers have joined DZone.

SKP's Java/JEE Gotchas: Testing JEE6 CDI Outside of a Container [Snippet]

DZone's Guide to

SKP's Java/JEE Gotchas: Testing JEE6 CDI Outside of a Container [Snippet]

If you're not ready to deploy to a container but want to test your CDI, you can do that in Java EE6 with Weld-SE and a bit of setup.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Today, we're going to look at a quick look at the trials and tribulations of testing whether your CDI is working in Java EE6. We're going to focus on testing outside a container, in case you aren't ready to go there yet — say in the initial stages.

Let's get started.

Add the Dependencies in Your Maven Model


Develop the Class(es) Which Are Annotated With CDI

 public class ContextsDependencyInjectionManager {  
      private static final Logger LOGGER = Logger.getLogger(ContextsDependencyInjectionManager.class);  
      private ManagedThreadFactory managedThreadFactory;  
      @Inject @Persistent
      private EngagementManager persistentManger;  
      @Inject @Radia
      private EngagementManager radiaManager;  
      @Inject @Accelerite  
      private EngagementManager acceleritManager;  
      @Inject @Services  
      private EngagementManager servicesManager;  

      ... // add functionality, methods, other atrributes

Write a Test Case to Test the Class(es) Using Weld-SE

 public class ContextsDependencyInjectionManagerTest {  
      public static void main(String[] args) throws Exception {  
           ContextsDependencyInjectionManagerTest test = new ContextsDependencyInjectionManagerTest();  

           // cdi initialization outside of container  
           Weld weld = new Weld();  
           WeldContainer weldContainer = weld.initialize();  
           ContextsDependencyInjectionManager cdiManager = weldContainer.instance()  
                     .select(ContextsDependencyInjectionManager .class).get();  

           ... // other tests, fire events, assert

Using/Firing Events in Weld-SE — Test @Observes

If you have used the Observer Pattern or added listeners using @Observes, you may choose to test them out as follows.

          .fire(new EngagementAttributeChangedEvent(  
            "contact", "sumith_puri", "pl"));  

This is helpful for developers doing testing before a version is available to deploy in a container. It's particularly useful for understanding if all your dependencies are getting injected and whether the CDI annotations are working as intended.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

cdi ,java ,weld ,jee6 ,tutorial

Published at DZone with permission of Sumith Puri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}