Over a million developers have joined DZone.

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

DZone's Guide to

SKP's Java/Java EE Gotchas: Testing 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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Today, we're going to look at a quick look at the trials and tribulations of testing whether your CDI is working in Java EE 6. 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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

cdi ,java ,weld ,jee6 ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}