16 December 2008

Setup Unit and Integration Test Environment using embedded JBoss for Maven based SEAM applications

This post explains how to setup and run unit and integration tests in a Maven based SEAM project. The server used is Tomcat [the same configuration is valid for JBoss but the scope of few dependencies should be made as provided. I will cover the setup with JBoss in the next post].We will also learn to run test coverage reports using Cobertura.

The dependencies required for embedded JBoss server are as below. Please copy these dependencies to your project POM.

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>jboss-embedded-all</artifactId>

            <version>beta3.SP4</version>

            <exclusions>

                <exclusion>

                    <groupId>org.jboss.embedded</groupId>

                    <artifactId>jboss-embedded</artifactId>

                </exclusion>

                  <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-client-spi</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-core-spi</artifactId>

                </exclusion>

            </exclusions>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>hibernate-all</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>thirdparty-all</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.jboss.embedded</groupId>

            <artifactId>jboss-embedded</artifactId>

            <version>beta3.SP4</version>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.jboss.microcontainer</groupId>

                    <artifactId>jboss-deployers-client-spi</artifactId>

                </exclusion>

              </exclusions>

        </dependency>

Please add the following for TestNG and el-api for parsing the expression language
<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>

            <version>5.7</version>
            <scope>compile</scope>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>

Download Embedded JBoss and unzip the following folders (that are under bootstrap folder) under src/test/resources of your application as in the screenshot . Note that I created WEB-INF folder and placed web.xml under it.
We need not copy any other jar files that are in the embedded JBoss as we have already specified them as dependencies in pom.xml.


Now comes the plugins to be added. The below will work both in J2SE 5.0  and Java SE 6.0.

            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
               
                    <execution>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax.xml.bind</groupId>
                                    <artifactId>jaxb-api</artifactId>
                                    <version>2.1</version>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.testOutputDirectory}/endorsed</outputDirectory>
                        </configuration>
                        <id>download-jaxb-api</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>

    
                    <childDelegation>true</childDelegation>
                    <useSystemClassLoader>true</useSystemClassLoader>
                    <argLine>-Djava.endorsed.dirs=${project.build.testOutputDirectory}/endorsed -Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <check>
                        <haltOnFailure>false</haltOnFailure>
                        <regexes>
                            <regex>
                                <pattern>com.thea.*</pattern>
                                <branchRate>90</branchRate>
                                <lineRate>90</lineRate>
                            </regex>
                    
                        </regexes>
                    </check>
                    <instrumentation>
                        <includes>
                            <include>com/thea/**/*.class</include>
                           
                        </includes>
                    </instrumentation>
                </configuration>
                <executions>
                    <execution>
                        <id>clean</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>instrument</id>
                        <phase>site</phase>
                        <goals>
                            <goal>instrument</goal>
                            <goal>cobertura</goal>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


To execute tests, please use mvn test command. To run cobertura please use mvn cobertura:cobertura

Now, when we try to run mvn test we may be greeted with NoSuchMethod error related to javassist. This is due to incompatible version of javassist that comes in as a result of transitive dependency of SEAM. Hence exclude them as below.

         <dependency>
            <groupId>org.jboss.seam</groupId>
            <artifactId>jboss-seam</artifactId>
            <version>2.1.1.CR2</version>
                     <exclusions>
            <exclusion>
            <groupId>javax.el</groupId>
                              <artifactId>el-api</artifactId>
            </exclusion>
                             <exclusion>
            <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
            </exclusion>