Home » Java » Enterprise Java » Integration tests with Maven 3, Failsafe and Cargo plugin

About Maciej Walkowiak

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"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


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

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";

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

      HttpResponse response = client.execute(httpget);


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.


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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.