Enterprise Java

Enable Arquillian on an existing Java EE project, using Forge Addon

Tech Tip #34 explained how to create a testable Java EE 7 application. This is useful if you are starting a new application. But what if you already have an application and Arquillian-enable it ?

That’s where Forge and Forge-Arquillian add-on comes in handy. That’s how I added support for Arquillian in javaee7-simple-sample.

Lets see what was done!
 
 
 

  1. Download and install Forge. You can download ZIP and unzip in your favorite location, or just use the following command that does it for you:
    Downloads> curl http://forge.jboss.org/sh | sh
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  2725    0  2725    0     0   4641      0 --:--:-- --:--:-- --:--:--  4642
    /usr/bin/java
    Downloading Forge
    ######################################################################## 100.0%
    Archive:  /Users/arungupta/.forge/forge_installer.zip
       creating: /Users/arungupta/forge/forge-distribution-2.12.2.Final/
       creating: /Users/arungupta/forge/forge-distribution-2.12.2.Final/img/
    
    . . .
    
    If you have not yet seen the Forge built-in commands, you may either press TAB to see a list of the currently available commands, or get a more descriptive list by typing:
    
    $ command-list
    
    Then to get started - see the docs at
    http://forge.jboss.org/documentation 
    Consider installing Git and Maven 3.1+ (both optional)
    
    Restart Terminal to use forge.
  2. Clone the simple-javaee7-sample repo:
    git clone https://github.com/javaee-samples/javaee7-simple-sample.git
  3. Change the directory to javaee7-simple-sample and start Forge:
    javaee7-simple-sample> ~/tools/forge-distribution-2.12.2.Final/bin/forge
    Using Forge at /Users/arungupta/tools/forge-distribution-2.12.2.Final
    
        _____                    
       |  ___|__  _ __ __ _  ___ 
       | |_ / _ \| `__/ _` |/ _ \  \\
       |  _| (_) | | | (_| |  __/  //
       |_|  \___/|_|  \__, |\___| 
                       |__/      
    
    JBoss Forge, version [ 2.12.2.Final ] - JBoss, by Red Hat, Inc. [ http://forge.jboss.org ]
  4. Install the Forge-Arquillian add-on:
    [javaee7-simple-sample]$ addon-install-from-git --url https://github.com/forge/addon-arquillian.git
    ***INFO*** Installing Addon from Git 	[0/4] ...
    ***INFO*** Installing Addon from Git:Cloning repository in /var/folders/3v/syxsk5zx3yqd_8g9m206py_h0000gn/T/1416131293813-0 	[0/4] ...
    ***INFO*** Installing Addon from Git:Installing project into local repository 	[1/4] ...
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Arquillian Forge Addon 1.0.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    
    . . .
    
    Downloading: http://repo1.maven.org/maven2/org/jboss/forge/addon/maven-impl-projects/2.12.2.Final/maven-impl-projects-2.12.2.Final.pom
    Downloaded: http://repo1.maven.org/maven2/org/jboss/forge/addon/maven-impl-projects/2.12.2.Final/maven-impl-projects-2.12.2.Final.pom (4 KB at 16.9 KB/sec)
    ***SUCCESS*** Addon org.arquillian.forge:arquillian-addon,1.0.0-SNAPSHOT was installed successfully.
  5. Configure Arquillian add-on and install WildFly adapter:
    [javaee7-simple-sample]$ arquillian-setup --testFramework junit --containerAdapter wildfly-remote 
    Downloading: http://repo1.maven.org/maven2/org/wildfly/wildfly-arquillian-container-remote/maven-metadata.xml
    Downloaded: http://repo1.maven.org/maven2/org/wildfly/wildfly-arquillian-container-remote/maven-metadata.xml (702 B at 4.2 KB/sec)
    ***SUCCESS*** Arquillian setup complete
    ***SUCCESS*** Installed Arquillian 1.1.5.Final
    ***SUCCESS*** Installed junit
    ***SUCCESS*** Installed Arquillian Container WildFly Remote 8.x
    ***SUCCESS*** Installed Arquillian Container WildFly Remote 8.x dependencies

    The list of adapters is diverse as shown:

    glassfish-embedded-3.1       jetty-embedded-6.1           tomee-remote                 
    glassfish-managed-3.1        jetty-embedded-7             was-embedded-8               
    glassfish-remote-3.1         openejb-embedded-3.1         was-remote-7                 
    jbossas-embedded-6           openejb-embedded-4           was-remote-8                 
    jbossas-managed-4.2          openshift-express            weld-ee-embedded-1.1         
    jbossas-managed-5.1          openwebbeans-embedded-1      weld-se-embedded-1           
    jbossas-managed-6            tomcat-embedded-6            weld-se-embedded-1.1         
    jbossas-managed-7            tomcat-embedded-7            wildfly-managed              
    jbossas-remote-4.2           tomcat-managed-5.5           wildfly-remote               
    jbossas-remote-5             tomcat-managed-6             wls-remote-10.3              
    jbossas-remote-5.1           tomcat-managed-7             
    jbossas-remote-6             tomcat-remote-6              

    This allows you to configure the container of your choice. This will add the following profile to your “pom.xml”:

    
        arquillian-wildfly-remote
        
            
                
                    maven-surefire-plugin
                    2.14.1
                    
                        
                            arquillian-wildfly-remote
                        
                    
                
            
        
        
            
                org.wildfly
                wildfly-arquillian-container-remote
                8.1.0.Final
                test
            
        
    

    The profile includes the “wildfly-arquillian-container-remote” dependency which allows Arquillian to connect with a WildFly running in remote “mode”. The default host is “localhost” and port is “8080”. The “maven-surefire-plugin” is passed a “arquillian.launch” configuration property with the value “arquillian-wildfly-remote”. This is matched with a “container” qualifier in the generated “arquillian.xml”.“arquillian.xml” is used to define configuration settings to locate or communicate with the container. In our case, WildFly is running on default host and port and so there is no need to update this file. The important part to note is that the “container” qualifier matches with the “arquillian.launch” qualifier value.

    
    
        
    

    This file. More details about this configuration file are available here.

  6. Until FORGE-2148 is fixed, you also need to add a JAX-RS implementation as well, and the corresponding JAXB provider. This test is using RESTEasy and so the following needs to be added:
    
        org.jboss.resteasy
        resteasy-client
        3.0.5.Final
        test
    
    
        org.jboss.resteasy
        resteasy-jaxb-provider
        3.0.5.Final
        test
    

    This can be added either in the profile or project-wide dependencies.

And now you are ready to test!

Download WildFly 8.1 and unzip. Start the server as:

./bin/standalone.sh

Run the tests:

javaee7-simple-sample> mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloworld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloworld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ helloworld ---
[INFO] Surefire report directory: /Users/arungupta/workspaces/javaee7-simple-sample/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.javaee7.sample.PersonTest
Nov 16, 2014 1:53:57 PM org.xnio.Xnio 
INFO: XNIO version 3.2.0.Beta4
Nov 16, 2014 1:53:57 PM org.xnio.nio.NioXnio 
INFO: XNIO NIO Implementation Version 3.2.0.Beta4
Nov 16, 2014 1:53:57 PM org.jboss.remoting3.EndpointImpl 
INFO: JBoss Remoting version (unknown)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.963 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.403 s
[INFO] Finished at: 2014-11-16T13:53:59+02:00
[INFO] Final Memory: 17M/309M
[INFO] ------------------------------------------------------------------------

And now you’ve Arquillian-enabled your existing project!

File any issues here.

Enjoy!

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button