Your browser (Internet Explorer 7 or lower) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.

X

Integration tests with Maven 3, Failsafe and Cargo plugin

Unit testing is available in Maven out of the box. Because of that very often its used for integration tests as well. Major disadvantage of this is that integration tests can take much more time to execute and because no one likes to wait long time every build – tests are just skipped with -Dmaven.test.skip=true flag

In order to execute integration tests with Maven we should use Maven Failsafe plugin. Thanks to that we can quickly run unit tests by calling mvn test or perform integration tests with mvn verify.

Integration tests should run in environment similar as much as its possible to production. If your application is a WAR or EAR package you can use Maven Cargo plugin in order to tell Maven to deploy it on a application server or servlet container and perform integration tests on deployed application.

Maven Failsafe plugin configuration

In order to enable integration test phase failsafe plugin configuration has to be added to pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.12</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
         </plugins>
    </build>
    ...
</project>

Now when mvn verify is called all files containing tests matches src/test/java/**/*IT.java will be executed during integration tests phase.

Integration tests are nothing but classes using JUnit or TestNG annotations to tell Maven which method is a test and should use same way of doing assertions like you do with unit tests.

Maven Cargo plugin configuration

Cargo plugin supports all major application server on the market. In my example I will use default Apache Tomcat 7 installation.

  • tomcat is being started in pre-integration phase
  • tomcat is being stopeed in post-integration phase
<plugin>
	<groupId>org.codehaus.cargo</groupId>
	<artifactId>cargo-maven2-plugin</artifactId>
	<version>1.2.0</version>
	<configuration>
		<container>
			<containerId>tomcat7x</containerId>
			<zipUrlInstaller>
				<url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
				</url>
				<downloadDir>${project.build.directory}/downloads</downloadDir>
				<extractDir>${project.build.directory}/extracts</extractDir>
			</zipUrlInstaller>
		</container>
	</configuration>
	<executions>
		<execution>
			<id>start-tomcat</id>
			<phase>pre-integration-test</phase>
			<goals>
				<goal>start</goal>
			</goals>
		</execution>
		<execution>
			<id>stop-tomcat</id>
			<phase>post-integration-test</phase>
			<goals>
				<goal>stop</goal>
			</goals>
		</execution>
	</executions>
</plugin>

It works pretty well. Now when you execute mvn verify for the first time you can see that Tomcat is being downloaded and started before integration tests run.

Integration test class example

Now we can finally write useful integration test – that will check if application sends correct error code in response.

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;

import java.io.IOException;

import static org.fest.assertions.Assertions.assertThat;

public class CheckApplicationDeployIT {
	private static final String URL = "http://localhost:8080/myApp/testURL";

	@Test
	public void testIfAppIsUp() throws IOException {
		//given
		HttpClient client = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(URL);

		//when
		HttpResponse response = client.execute(httpget);

		//then
		assertThat(response.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_OK);
	}
}

Of course integration tests should be more complex and actually test behavior. Right now you can setup Waitr, Selenium or any other solution that fits the best your needs and create real integration tests.

Conclusion

Do you always should test deployed application in integration tests? Its very useful but not always. If your application depends somehow on user’s ip address you will not be able to change it in different requests.

But if your application is a classic web app with HTML or REST frontend – then its highly recommended.

If you enjoyed this post, then make sure you subscribe to my RSS feed

  • http://michalorman.pl/ Michał Orman

    Correct me if I’m wrong, but shouldn’t carg-maven2-plugin configuration contain deployable(s) apart of just container?

    What I like to do is to put functional/integration tests into separate module and include this module in maven profile. This way tests are not executed during each build (and potentially tomcat is not being downloaded), but only when I want them to run. However this configuration is problematic when using Eclipse (I’m using IDEA and for me it is not a problem).

  • MaciejWalkowiak

    Could you explain what do you mean by “configuration contain deployable(s) apart of just container”?

    Thanks to Failsafe plugin integration tests are not executed every build. They are executed only if you perform mvn verify or mvn install. When you do mvn package failsafe is not invoked. 

    Tomcat also does not need to be downloaded every time. You don’t even need to specify url to Tomcat installation but you can just set location of Tomcat on your hard drive. 

    • http://michalorman.com/ Michał Orman

      I mean something like this:

      net.netm.taiyo
      taiyo
      war

      taiyo

      This is also mentioned on cargo site: http://cargo.codehaus.org/Starting+and+stopping+a+container

      Otherwise (at least on my project setup) tomcat is started, but no application is running on it while nothing is being deployed to the server. The deployables part declares what should be deployed. I don’t know if this is required only in my setup (where integration-tests are separated) but I assumed that every cargo setup should declare deployables.

      Regarding the tomcat downloading. If you have integration tests setup done in your web module (not separate module) than if you do `mvn clean package/install` tomcat will be removed and downloaded again unless you put it somewhere else than ${project.build.directory}. It is a matter of preference if you do separate module for integration test, separate test sources like main/it/java or you put tests in main/test/java but postfix them with *IT.

      My setup was motivated with this book:
      https://repo.maestrodev.com/books/betterbuildswithmaven-2008.pdf

  • Mehdi Heidarzadeh

    Thanks Maciej. good post.
    I have question, how do you compare cargo and arquillian which is a test framework that can be used to perform testing inside a remote or embedded container, or deploy an archive to a container?

    • http://maciejwalkowiak.pl/ Maciej Walkowiak

      Thanks. I can’t really compare them because I haven’t got yet opportunity to test Arquillian. I may be wrong but Cargo lets just deploy or app in order to make end to end tests without diving into application details. With Arquillian lets you test components, objects inside deployed container. So Arquillian sounds more like Spring Test Framework.