After we have finished this lesson, we:
- Can configure the source and resource directories of our integration tests.
- Understand how we can skip either unit or integration tests by using Maven profiles.
- Know how we can run integration tests which use Spock Framework.
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/groovy 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.
Let’s start by creating the Maven profiles that are used to run our unit and integration tests.
Creating the Required Maven Profiles
We have to create two Maven profiles that are described in the following:
- The
devprofile is the default profile of our Maven build and it ensures that only unit tests are run when thedevprofile is active. - The
integration-testprofile ensures that only integration tests are run when theintegration-testprofile 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.testsproperty istrueif Maven should skip our unit tests. Because only unit tests are run by default, the default value of this property must befalse. - The
skip.integration.testsproperty istrueif Maven should skip our integration tests. The default value of this property must betrue.
After we added these properties to 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:
- We have to create a profile called:
devand ensure that this profile is active by default. - We have to create a profile called:
integration-testand ensure that Maven runs only our integration tests when this profile is active.
After we have added these profiles to 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>
Next, we have to configure the source directories of our unit and integration tests.
Configuring the Source Directories of Our Unit and Integration Tests
Because we want to write both unit and integration tests by using the Groovy programming language, the source directories of our unit and integration tests must be recognized by the GMavenPlus plugin. That’s why we cannot configure the source directory of our integration tests by using the Build Helper Maven plugin. Instead, we have to make the following changes to the configuration of the GMavenPlus plugin:
- Ensure that it compiles our unit tests which are found from the src/test/groovy directory.
- Ensure that it compiles our integration tests which are found the src/integration-test/groovy directory.
After we have made the required changes to the configuration of the GMavenPlus plugin, its configuration looks as follows:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<configuration>
<testSources>
<testSource>
<directory>src/test/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
<testSource>
<directory>src/integration-test/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
After we have configured the source directories of our unit and integration tests, we have to configure the resource directory of our integration tests. Let’s find out how we can do it.
Configuring the Resource Directory of Our Integration Tests
We can add additional resource directories to our Maven build by using the Build Helper Maven plugin. Of course, before we can use this, we have to declare it in our POM file.
We can do this by adding the following snippet to the plugins section of our pom.xml file:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.12</version> </plugin>
We can now add the required resource directory to our Maven build by following these steps:
First, we have to add the executions section to 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 resource directory of our integration tests (src/integration-test/resources) to 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-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/integration-test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Next, we have to ensure that Maven runs our unit tests 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 property 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 property 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.20</version>
<configuration>
<groups>com.testwithspring.master.UnitTest</groups>
<includes>
<include>**/*Spec.java</include>
</includes>
<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 GetMessageSpec class contains one feature method which ensures that the getMessage() method of the MessageService class returns the correct message. Also, because we want to configure the category of this specification class, we have to annotate it with the @Category annotation.
The source code of our specification class looks as follows:
import org.junit.experimental.categories.Category
import spock.lang.Specification
@Category(IntegrationTest.class)
class GetMessageSpec extends Specification {
def messageService = new MessageService()
def 'Get message'() {
expect: 'Should return the correct message'
messageService.getMessage() == 'Hello World!'
}
}
The IntegrationTest interface is a marker interface that identifies our integration tests. Its source code looks as follows:
interface IntegrationTest {
}
Let’s move on and find out how we can run our integration tests with Maven.
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 to the plugins section:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20</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.20</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.20</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Third, we have to ensure that the Maven Failsafe plugin 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 property.
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.20</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<groups>com.testwithspring.master.IntegrationTest</groups>
<includes>
<include>**/*Spec.java</include>
</includes>
</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 property 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 property 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.20</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<groups>com.testwithspring.master.IntegrationTest</groups>
<includes>
<include>**/*Spec.java</include>
</includes>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
Next, we will 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:
[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.testwithspring.master.MessageServiceSpec [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 s - in com.testwithspring.master.MessageServiceSpec
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] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.testwithspring.master.GetMessageSpec [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.091 s - in com.testwithspring.master.GetMessageSpec
Let’s summarize what we learned from this lesson.
Summary
This lesson has taught us four things:
- We have to configure the source directories of our unit and integration tests by changing the configuration of the GMavenPlus plugin.
- We have to configure the resource directory of our integration tests by using the Build Helper Maven plugin.
- We can run our integration tests by using the Maven Failsafe plugin.
- We can skip unit and integration tests by setting the value of the
skipTestsproperty of the Maven Surefire and Maven Failsafe plugins totrue.