Integration Testing

Introduction

Some common strategies for handling Integration/Functional testing with Maven are outlined in the document Maven and Integration Testing.

To summarize, there are two basic strategies:

  1. Using a Separate Module for Integration Tests(the recommended practice)
  2. Other Strategies (the work-arounds)

1 Using a Separate Module for Integration Tests

See BetterBuildsWithMaven-1.0.2 (Version 1.0.2 © Oct. 2006 Mergere, Inc.), section 4.13 “Testing J2EE Application” for a discussion and realistic example of this method.

In order to test this approach we took the simpleWebApp developed earlier and investigated modifying it to add integration tests using this approach. Below we outline the steps we followed:

  • We took the simpleWebApp project and created a copy of it, which we renamed to webAppWithIntegrationTests. This will be the container for two sub-modules, one for the web application and one for the integration tests.
  • we made the following edits to the pom.xml file of this project
    • changed the name of the project to: webAppWithIntegrationTests
    • changed version to: 1.0-SNAPSHOT
    • changed the packaging element to pom
  • cd to the webAppWithIntegrationTests directory
  • create the sub-module which will contain the web application by running:
    mvn archetype:create -DartifactId=web               \
         -DgroupId=ca.intelliware.maven-examples
  • create the sub-module which will contain the integration tests running:
    mvn archetype:create -DartifactId=integration-tests \
         -DgroupId=ca.intelliware.maven-examples
  • move the “web” and “integration-tests” directories into the parent directory, so that they will be siblings of webAppWithIntegrationTests
  • Update the webAppWithIntegrationTests/pom.xml to reflect the new location of these projects, as follows:
    <modules>
      <module>../web</module>
      <module>../integration-tests</module>
    </modules>
  • move src directory from webAppWithIntegrationTests to web
  • add the following to the web project’s pom.xml: <packaging>war</packaging>
  • delete directory C:workspacemavenintegration-testssrcmain
  • delete C:workspacemavenintegration-testssrctestAppTest.java
  • move:
    <build>
    	<finalName>${artifactId}</finalName>
    	<plugins>
    		<plugin>
    			<groupId>org.mortbay.jetty</groupId>
    			<artifactId>maven-jetty-plugin</artifactId>
    			<version>6.1.0pre3</version>
    			<configuration>
    				<contextPath>/</contextPath>
    				<scanIntervalSeconds>10</scanIntervalSeconds>
    			</configuration>
    		</plugin>
    	</plugins>
    </build>

    to the web project.

  • run mvn eclipse:eclipse in the webAppWithIntegrationTests directory
  • mvn compile should now work
  • mvn install
  • run mvn org.mortbay.jetty:maven-jetty-plugin:run
  • hit the page with: http://localhost:8080/
  • now write some integration tests and execute them by running “mvn integration-test”.

2 Other Strategies (the work-arounds)

If for some reason you cannot move your integration tests into a separate module, then there are two common alternative solutions.

2.1 Integration tests with no unit tests in a module

If you have only integration tests in the same module as your webapp, i.e. you do not have any unit tests in this module, then there is a simple solution. You can simply configure Surefire to skip the test phase (i.e. that runs unit tests), and to run all your tests in the integration-test phase instead. This can be accomplished via a fragment like the following in your project’s POM file:

<!-- skip unit test run, all tests are to be executed during integration-test -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
	  <skip>true</skip>
	</configuration>
	<executions>
	  <execution>
		<id>surefire-it</id>
		<phase>integration-test</phase>
		<goals>
		  <goal>test</goal>
		</goals>
		<configuration>
		  <skip>false</skip>
		</configuration>
	  </execution>
	</executions>
  </plugin>

2.2 Integration tests and unit tests in the same module

If you have both unit tests and integration tests in your module then you have a problem as Maven currently only allows a single source directory for tests (src/test/java by default).

This is a common complaint and may be resolved in future versions. In the meantime, the standard solution is to place your integration tests into a location in your package heirarchy, typically something like “**/systest/*.java”.

This solution is described in more detail in: http://wiki.rodcoffin.com/index.php?title=Maven_Integration_Testing.

The key is to exclude your integration tests from the default configuration for surefire (which will run the unit tests) and then to define a run of surefire that will occur in the integration-test phase. You will need to step that execution to override the default exclusion and to define an inclusion of just the integration tests.

To summarize, your POM file will need a fragment like the following:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
	   <excludes>
		  <exclude>**/systest/*.java</exclude>
	   </excludes>
	</configuration>
	<executions>
	   <execution>
		  <id>integration-test</id>
		  <goals>
			 <goal>test</goal>
		  </goals>
		  <phase>integration-test</phase>
		  <configuration>
			 <excludes>
				<exclude>none</exclude>
			 </excludes>
			 <includes>
				<include>**/systest/*.java</include>
			 </includes>
		  </configuration>
	   </execution>
	</executions>
</plugin>

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply