DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. 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 · Code Snippet
Like (8)
Save
Tweet
Share
9.38K 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

  • Public Cloud-to-Cloud Repatriation Trend
  • Spring Boot Docker Best Practices
  • Using QuestDB to Collect Infrastructure Metrics
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: