Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

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

<dependency>  
    <groupId>org.jboss.weld.se</groupId>  
    <artifactId>weld-se-core</artifactId>  
    <version>2.2.9.Final</version>  
    <scope>test</scope>  
</dependency>


Develop the Class(es) Which Are Annotated With CDI

@ApplicationScoped  
 public class ContextsDependencyInjectionManager {  
      private static final Logger LOGGER = Logger.getLogger(ContextsDependencyInjectionManager.class);  
      @Resource  
      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.

weldContainer  
          .event()  
          .select(EngagementAttributeChangedEvent.class)  
          .fire(new EngagementAttributeChangedEvent(  
            EngagementManagerConstants.ENGAGEMENT_MANAGER_ENTITY_DASHBOARD,  
            "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.

Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

Topics:
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 }}