Test With Spring Course
Save Time by Writing Less Test Code
  • Home
  • Pre-Sales FAQ
  • Support
  • Log In

/ October 8, 2016 / petrikainulainen

Running Integration Tests With Maven

Lesson Progress:
← Back to Topic

After we have finished this lesson, we:

  • Can add custom source and resource directories into our Maven build.
  • Know how we can run our integration tests with Maven.
  • Understand how we can skip either unit or integration tests by using Maven profiles.

This lesson assumes that:

  • You are familiar with JUnit 4
  • You can use custom test runners
  • You know how you can run unit tests by using Maven

Watch the Lesson

 

The text version of this lesson is given in the following:

The Requirements of Our Maven Build

The requirements of our Maven build are:

  • Our integration tests must have a separate source directory. The src/integration-test/java directory must contain the source code of our integration tests.
  • Our integration tests must have a separate resource directory. The src/integration-test/resources directory must contain the resources of our integration tests.
  • Only unit tests are run by default.
  • We must be able to run only integration tests.
  • The names of our integration test classes don’t have to start or end with a special marker string.

Let’s start by creating Maven profiles that are used to run our unit and integration tests.

Creating Maven Profiles for Unit and Integration Tests

We need to create two Maven profiles that are described in the following:

  • The dev profile is the default profile of our Maven build and it ensures that only unit tests are run when the dev profile is active.
  • The integration-test profile ensures that only integration tests are run when the integration-test profile is used.

We can create these Maven profiles by following these steps:

First, we have to add two properties to the properties section of our POM file:

  • The skip.unit.tests property is true if Maven should skip our unit tests. Because only unit tests are run by default, the default value of this property must be false.
  • The skip.integration.tests property is true if Maven should skip our integration tests. The default value of this property must be true.

After we added these properties into our POM file, its properties section looks as follows:

<properties>
	<skip.unit.tests>false</skip.unit.tests>
	<skip.integration.tests>true</skip.integration.tests>
</properties>

Second, we have to create the required Maven profiles by following these steps:

  1. We have to create a profile called: dev and ensure that this profile is active by default.
  2. We have to create a profile called: integration-test and ensure that Maven runs only our integration tests when this profile is active.

After we have added these profiles into our Maven build, our POM file should have the following profile configuration:

<profiles>
	<profile>
		<id>dev</id>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
	</profile>
	<profile>
		<id>integration-test</id>
		<properties>
			<skip.integration.tests>false</skip.integration.tests>
			<skip.unit.tests>true</skip.unit.tests>
		</properties>
	</profile>
</profiles>

Our next step is to configure the source and resource directories of our integration tests.

Adding Source and Resource Directories Into Our Maven Build

We can add additional source and resource directories into our Maven build by using the Build Helper Maven Plugin. Of course, before we can use it, we have to declare this plugin in our POM file.

We can do this by adding the following snippet into the plugins section:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>1.12</version>
</plugin>

We can now add the required source and resource directories into our Maven build by following these steps:

First, we have to add the executions section in the declaration of the Build Helper Maven Plugin. After we have added this section, the configuration of the Build Helper Maven Plugin looks as follows:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>1.12</version>
	<executions>
	</executions>
</plugin>

Second, we have to create an execution that adds the source directory of our integration tests (src/integration-test/java) into our Maven build. This execution must invoke the add-test-source goal of the Build Helper Maven Plugin when the generate-test-sources phase of the Maven default lifecycle is invoked.

After we have created this execution, the configuration of the Build Helper Maven Plugin looks as follows:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>1.12</version>
	<executions>
		<execution>
			<id>add-integration-test-sources</id>
			<phase>generate-test-sources</phase>
			<goals>
				<goal>add-test-source</goal>
			</goals>
			<configuration>
    			<sources>
    				<source>src/integration-test/java</source>
    			</sources>
			</configuration>
		</execution>
	</executions>
</plugin>

Third, we have to create an execution that adds the resource directory of our integration tests (src/integration-test/resources) into our Maven build. This execution must invoke the add-test-resource goal of the Build Helper Maven Plugin when the generate-test-resources phase of the Maven default lifecycle is invoked.

After we have created this execution, the configuration of the Build Helper Maven Plugin looks as follows:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>1.12</version>
	<executions>
		<execution>
			<id>add-integration-test-sources</id>
			<phase>generate-test-sources</phase>
			<goals>
				<goal>add-test-source</goal>
			</goals>
			<configuration>
    			<sources>
    				<source>src/integration-test/java</source>
    			</sources>
			</configuration>
		</execution>
		<execution>
			<id>add-integration-test-resources</id>
			<phase>generate-test-resources</phase>
			<goals>
				<goal>add-test-resource</goal>
			</goals>
			<configuration>
				<resources>
					<resource>
						<filtering>true</filtering>
						<directory>src/integration-test/resources</directory>
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>

Additional Reading:

  • Build Helper Maven Plugin – Plugin Documentation
  • Build Helper Maven Plugin – Usage

Next, we have to ensure that our unit tests are run only when the dev Maven profile is active.

Configuring Maven to Skip Our Unit Tests

The Maven Surefire Plugin doesn’t run unit tests when the value of its skipTests configuration option is true. Because we want to run our unit tests only when the dev profile is active, we have to configure Maven to read the value of this configuration option from the value of the skip.unit.tests property.

After we have done this, the configuration of the Maven Surefire Plugin looks as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.19.1</version>
	<configuration>
		<groups>com.testwithspring.intermediate.unittests.UnitTest</groups>
		<skipTests>${skip.unit.tests}</skipTests>
	</configuration>
</plugin>

Before we can configure Maven to run our integration tests, we have to write one integration test.

Writing a Simple Integration Test

The SampleIntegrationTest class contains one integration test that writes the string: ‘The category: IntegrationTest’ to System.out. Because we want to configure the category of this test class, we have to annotate it with the @Category annotation.

The source code of our test class looks as follows:

import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(IntegrationTest.class)
public class SampleIntegrationTest {

    @Test
    public void sampleIntegrationTest() {
        System.out.println("Category: IntegrationTest");
    }
}

The IntegrationTest interface is a marker interface that identifies our integration tests. Its source code looks as follows:

public interface IntegrationTest {
}

Let’s move on and configure Maven to run our integration tests.

Running Integration Tests With Maven

We can run integration tests with Maven by using the Maven Failsafe Plugin. Our first step is to declare this plugin in our POM file.

We can do this by adding the following snippet into the plugins section:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.19.1</version>
</plugin>

We can now configure this plugin by following these steps:

First, we have ensure that both integration-test and verify goals of this plugin are run. The integration-test goal runs our integration tests and the verify goal checks the results of our integration tests and fails the build if our integration tests failed.

After we have configured these goals, the configuration of the Maven Failsafe Plugin looks as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.19.1</version>
	<executions>
		<execution>
			<id>integration-tests</id>
			<goals>
				<goal>integration-test</goal>
				<goal>verify</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Second, we have to ensure that the Maven Failsafe Plugin scans all source files when it looks for integration tests. After we have done this, our configuration looks as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.19.1</version>
	<executions>
		<execution>
			<id>integration-tests</id>
			<goals>
				<goal>integration-test</goal>
				<goal>verify</goal>
			</goals>
			<configuration>
				<includes>
					<include>**/*.java</include>
				</includes>
			</configuration>
		</execution>
	</executions>
</plugin>

Third, we have to ensure that Maven Failsafe Plugins runs all tests that belong to the IntegrationTest category. We can specify the used category by setting the fully qualified class name of our category interface as the value of the groups configuration option.

After we have configured the used category, the configuration of the Maven Failsafe Plugin looks as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.19.1</version>
	<executions>
		<execution>
			<id>integration-tests</id>
			<goals>
				<goal>integration-test</goal>
				<goal>verify</goal>
			</goals>
			<configuration>
				<includes>
					<include>**/*.java</include>
				</includes>
				<groups>com.testwithspring.intermediate.integrationtests.IntegrationTest</groups>
			</configuration>
		</execution>
	</executions>
</plugin>

Fourth, we have to ensure that the Maven Failsafe Plugin runs integration tests only if the integration-test Maven profile is active. The Maven Failsafe Plugin doesn’t run integration tests when the value of its skipTests configuration option is true.

Because we want to run our integration tests only if the integration-test profile is active, we have to configure Maven to read the value of this configuration option from the value of the skip.integration.tests property.

After we have done this, the configuration of the Maven Failsafe Plugin looks as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.19.1</version>
	<executions>
		<execution>
			<id>integration-tests</id>
			<goals>
				<goal>integration-test</goal>
				<goal>verify</goal>
			</goals>
			<configuration>
				<includes>
					<include>**/*.java</include>
				</includes>
				<groups>com.testwithspring.intermediate.integrationtests.IntegrationTest</groups>
				<skipTests>${skip.integration.tests}</skipTests>
			</configuration>
		</execution>
	</executions>
</plugin>

Additional Reading:

  • Maven Failsafe Plugin – Plugin Documentation
  • Maven Failsafe Plugin – Usage

Let’s move on and find out how we can run our unit and integration tests.

Running Our Unit and Integration Tests

We can run our unit tests by using the following command:

mvn clean test -P dev

When we run this command at command prompt, we can see that only unit tests are run:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.testwithspring.intermediate.unittests.SampleUnitTest
The category: UnitTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - 
in com.testwithspring.intermediate.unittests.SampleUnitTest

We can run our integration tests by using the following command:

mvn clean verify -P integration-test

When we run this command at command prompt, we can see that only integration tests are run:

[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ 
running-integration-tests ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-failsafe-plugin:2.19.1:integration-test 
(integration-tests) @ running-integration-tests ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.testwithspring.intermediate.integrationtests.SampleIntegrationTest
Category: IntegrationTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - 
in com.testwithspring.intermediate.integrationtests.SampleIntegrationTest

Let’s summarize what we learned from this lesson.

Summary

This lesson has taught us three things:

  • We can add custom source and resource directories into our Maven build by using the Build Helper Maven Plugin.
  • We can run our integration tests by using the Maven Failsafe Plugin.
  • We can skip integration and unit tests by setting the value of the skipTests configuration option of the Maven Surefire and Maven Failsafe plugins to true.

Get the source code from Github

Next Lesson →

Can I help you?

This is a free sample lesson of my Test With Spring course. If this lesson helped you to solve your problem, you should find out how my testing course can help you.

Support and Privacy

  • Pre-Sales FAQ
  • Support
  • Cookie Policy
  • No Bullshit Privacy Policy
  • No Bullshit Terms and Conditions

Test With Spring Course

  • Starter Package
  • Intermediate Package
  • Master Package

Free Sample Lessons

  • Introduction to JUnit 4
  • Introduction to Unit Testing
  • Introduction to Integration Testing
  • Introduction to End-to-End Testing
  • Introduction to Spock Framework
  • Introduction to Integration Testing – Spock Edition
  • Writing End-to-End Tests With Spock Framework

Copyright Koodikupla Oy 2016 — Built on Thesis by Themedy

This website collects some information from you. The collected information is described on the privacy policy.CloseRead Privacy Policy