Showing posts with label container. Show all posts
Showing posts with label container. Show all posts

Wednesday, April 15, 2015

Debugging using Wildfly and Arquillian

This should be most useful for those starting out with Arquillian! I spent quite some time before finding out the solution.

The arquillian.xml should include the following contents:
 <engine>  
     <property name="deploymentExportPath">target/</property>  
   </engine>  
   <container qualifier="jbossas-managed-wildfly-8" default="true">  
        <protocol type="jmx-as7">  
       <property name="executionType">REMOTE</property>  
     </protocol>  
     <configuration>  
          <property name="allowConnectingToRunningServer">true</property>  
       <property name="jbossHome">C:\Program Files (x86)\PSL\RCA\ApplicationServer</property>  
       <property name="modulePath">C:\Program Files (x86)\PSL\RCA\ApplicationServer\modules</property>  
       <property name="javaVmArguments">-Xrunjdwp=transport=dt_socket,address=8000,server=y,suspend=n -Djava.util.logging.manager=org.jboss.logmanager.LogManager</property>  
     </configuration>  
   </container>  


Include the following profile in your Maven pom.xml,
  <profile>  
   <id>arquillian-wildfly-managed</id>  
   <activation>  
        <activeByDefault>true</activeByDefault>  
        </activation>    
   <dependencies>  
      <dependency>  
        <groupId>org.jboss.spec</groupId>  
        <artifactId>jboss-javaee-6.0</artifactId>  
        <version>3.0.1.Final</version>  
        <type>pom</type>  
        <scope>test</scope>  
      </dependency>  
      <!-- Required by jboss-javaee-6.0:3.0.2.Final (https://issues.jboss.org/browse/JBBUILD-708) -->  
      <dependency>  
        <groupId>xalan</groupId>  
        <artifactId>xalan</artifactId>  
        <version>2.7.1</version>  
        <scope>test</scope>  
      </dependency>  
      <dependency>  
        <groupId>org.wildfly</groupId>  
        <artifactId>wildfly-arquillian-container-managed</artifactId>  
        <version>8.1.0.Final</version>  
        <scope>test</scope>  
      </dependency>  
      </dependencies>  
   </profile>  

When we use Arquillian, there are three VM's that are involved:
 
1. Arquillian (Source) > Remote/Managed Server
Start the Remote/Managed Server with the following line added to standalone.conf.bat
 set "JAVA_OPTS=%JAVA_OPTS% -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"  

In Eclipse, Right Click on your project and Click on "Debug" and under "Connect" - Select your "Host" and "Port". Click on "Apply" and "Debug". This will connect the Debugger to the Remote VM inside Wildfly. 


2. Test > Remote/Managed Server
Now, Right Click on your Arquillian Test and Click on "Debug As.." or "Debug Configurations". Then select either "TestNG" or "JUnit". 

Now, You will be able to hit the Breakpoint. Happy Debugging!

Monday, March 23, 2015

Testing JEE6 CDI outside of Container using Weld-SE




0. Add the dependencies in Maven Model
    <dependency>  
       <groupId>org.jboss.weld.se</groupId>  
       <artifactId>weld-se-core</artifactId>  
       <version>2.2.9.Final</version>  
       <scope>test</scope>  
    </dependency>  

1. 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 }

2. 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 }

3.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 the initial developer testing and before a version is available to deploy in the container, to understand if all dependencies are getting injected and whether many of the CDI annotations are working as intended.