Cheatsheet:  Testing with Gradle, Arquillian and WildFly

Version 2

    Overview

     

    This document outlines what you need to setup your Gradle project to use Arquillian and Wildfly for testing.  In this scenario, WildFly is "embedded" - run in the same JVM.

     

    Versions

     

    Gradle 1.11

    WildFly 8.0.0.Final

    Java 1.7

     

    Project Setup

     

    test
    |-- bin
    |   `-- test
    |       |-- TestCDI.class
    |       `-- TestEJB.class
    |-- build.gradle
    |-- src
    |   `-- test
    |       `-- java
    `-- wildfly-8.0.0.Final
    
    
    
    
    
    

     

    As mentioned above, it needs a copy of WildFly to run the test.

     

    build.gradle

     

    description = 'test project'
    
    ext.jbossHome = hasProperty('jbossHome') ? jbossHome : "${rootDir}/wildfly-8.0.0.Final"
    
    apply plugin: 'java'
    
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    
    repositories {
        maven { url 'https://repository.jboss.org/nexus/content/groups/public-jboss' }
        maven { url 'https://repository.jboss.org/nexus/content/repositories' }
        maven { url 'https://repository.jboss.org/nexus/content/repositories/thirdparty-releases' }
        mavenCentral()
    }
    
    
    dependencies {
        compile 'org.wildfly:wildfly-spec-api:8.0.0.Final'
        compile 'org.slf4j:slf4j-simple:1.7.7'
    
        testCompile 'junit:junit:4.11'
    
        testCompile 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-depchain:2.1.0'
        testCompile 'org.jboss.arquillian:arquillian-bom:1.1.2.Final-wildfly-1'
        testCompile 'org.jboss.arquillian.junit:arquillian-junit-container:1.1.2.Final-wildfly-1'
    
        testRuntime 'org.jboss.logging:jboss-logging:3.1.4.GA'
        /**
         * Embedded means it runs in the same JVM, it still needs the full installation
         */
        testRuntime 'org.wildfly:wildfly-arquillian-container-embedded:8.0.0.Final'
    }
    
    test {
        jvmArgs '-XX:MaxPermSize=256m'
    
        /**
         * Installation of JBOSS to use for testing, this can be set here
         * or in the arquillian.xml.
         */
        environment 'JBOSS_HOME', rootProject.jbossHome
        /**
         * WildFly embedded default logging
         */
        systemProperty 'java.util.logging.manager', 'org.jboss.logmanager.LogManager'
    }
    
    
    
    
    

     

     

    TestCDI and TestEJB

     

    @RunWith(Arquillian.class)
    public class TestCDI {
    
        Logger log = LoggerFactory.getLogger(TestCDI.class);
    
        @Inject
        private TestEJB testEjb;
    
        @Deployment
        public static WebArchive createDeployment() {
            File[] dependencies = Maven.resolver().resolve(
                    "org.slf4j:slf4j-simple:1.7.7"
                    )
                    .withoutTransitivity().asFile();
      
      
            WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
                    .addPackage(TestCDI.class.getPackage())
                    .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    
            war.addAsLibraries(dependencies);
      
            return war;
        }
    
        @Test
        public void testCDI() throws Exception {
            log.info(testEjb.helloWorld());
        }
    
    
    
    
    

     

     

    @Stateless
    @LocalBean
    public class TestEJB {
    
        public String helloWorld() {
            return "hello world";
        }
    }
    
    
    
    
    

     

    Running test

    ➜  test  gradle clean test
    :clean UP-TO-DATE
    :compileJava UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    :compileTestJava
    warning: Supported source version 'RELEASE_6' from annotation processor 'org.eclipse.sisu.space.SisuIndexAPT6' less than -source '1.7'
    1 warning
    :processTestResources UP-TO-DATE
    :testClasses
    :test
    INFO  [org.jboss.modules] JBoss Modules version 1.3.0.Final
    INFO  [org.jboss.msc] JBoss MSC version 1.2.0.Final
    INFO  [org.jboss.as] JBAS015899: WildFly 8.0.0.Final "WildFly" starting
    INFO  [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
    INFO  [org.xnio] XNIO version 3.2.0.Final
    INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.2.0.Final
    INFO  [org.jboss.remoting] JBoss Remoting version 4.0.0.Final
    INFO  [org.jboss.as.jacorb] JBAS016300: Activating JacORB Subsystem
    ...
    ...
    INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] JNDI bindings for session bean named TestEJB in deployment unit deployment "test.war" are as follows:
    
            java:global/test/TestEJB!test.TestEJB
            java:app/test/TestEJB!test.TestEJB
            java:module/TestEJB!test.TestEJB
            java:global/test/TestEJB
            java:app/test/TestEJB
            java:module/TestEJB
    
    INFO  [org.jboss.weld.deployer] JBAS016005: Starting Services for CDI deployment: test.war
    INFO  [org.jboss.weld.Version] WELD-000900: 2.1.2 (Final)
    INFO  [org.jboss.weld.deployer] JBAS016008: Starting weld service for deployment test.war
    INFO  [org.wildfly.extension.undertow] JBAS017534: Registered web context: /test
    INFO  [org.jboss.as.server] JBAS018559: Deployed "test.war" (runtime-name : "test.war")
    ...
    ...
    INFO  [org.jboss.as] JBAS015950: WildFly 8.0.0.Final "WildFly" stopped in 140ms
    
    BUILD SUCCESSFUL
    
    Total time: 17.98 secs
    
    
    
    
    

     

     

    Look at our log for result

     

    ➜  test  more wildfly-8.0.0.Final/standalone/log/server.log
    2014-04-29 12:33:48,777 INFO  [test.TestCDI] (default task-1) hello world
    2014-04-29 12:44:59,142 INFO  [test.TestCDI] (default task-2) hello world
    
    
    

     

     

    Arquillian.xml

     

    I did not use arquillian.xml in this test but here is a sample if you are interested.  Like I mentioned in the build.gradle you can put the location of wildfly here or as a system property.  Look also at the commented supported property names, everything else you can control by modifying standalone.xml

     

    <?xml version="1.0" encoding="UTF-8"?>
    <arquillian xmlns="http://jboss.org/schema/arquillian"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    
        <container qualifier="jboss" default="true">
            <configuration>
            <!-- Supported property names: [managementPort, username, managementAddress, bundlePath, managementProtocol, 
                        cleanServerBaseDir, jbossHome, password, modulePath] -->
                <property name="jbossHome">wildfly-8.0.0.Final/</property>
            </configuration>
        </container>
    </arquillian>
    

     

     

    Happy Testing