DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > SKP's Java/Java EE Gotchas: Testing CDI Outside of a Container [Snippet]

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.

Sumith Puri user avatar by
Sumith Puri
·
Feb. 10, 17 · Java Zone · Code Snippet
Like (8)
Save
Tweet
9.15K Views

Join the DZone community and get the full member experience.

Join For 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 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.

CDI Container

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Implementing Microservices Architectures
  • 5 Ways to Optimize Your CQL Queries for Performance
  • Building a QR Code Generator with Azure Functions
  • How Do You Integrate Emissary Ingress With OPA?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo