This article looks at how to segregate different types of tests in a build using Maven. There are several way in achieving this goal. However, this tutorial looks at a common approach that uses the following two Maven plugins.
maven-failsafe-plugin
build-helper-maven-plugin
Default Project Structure:
project-root
│
├── pom.xml
└── src
├── main
│ ├── java
│ └── resources
└── test
├── java
└── resources
Updated Project Structure:
project-root
│
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── Something.java
│ │ └── resources
│ │ └── prod-resource.txt
│ └── test
│ ├── acceptance
│ │ ├── java
│ │ │ └── SomethingAcceptanceTests.java
│ │ └── resources
│ │ └── acceptance-resource.txt
│ ├── integration
│ │ ├── java
│ │ │ └── SomethingIntegrationTests.java
│ │ └── resources
│ │ └── integration-resource.txt
│ └── unit
│ ├── java
│ │ └── SomethingTest.java
│ └── resources
│ └── unit-resource.txt
└── target
├── classes
│ ├── Something.class
│ └── prod-resource.txt
├── failsafe-reports
│ ├── SomethingAcceptanceTests.txt
│ ├── SomethingIntegrationTests.txt
│ ├── TEST-SomethingAcceptanceTests.xml
│ ├── TEST-SomethingIntegrationTests.xml
│ └── failsafe-summary.xml
├── maven-archiver
│ └── pom.properties
├── some.artifact.id-1.0-SNAPSHOT.jar
├── surefire-reports
│ ├── SomethingAcceptanceTests.txt
│ ├── SomethingIntegrationTests.txt
│ ├── SomethingTest.txt
│ ├── TEST-SomethingAcceptanceTests.xml
│ ├── TEST-SomethingIntegrationTests.xml
│ └── TEST-SomethingTest.xml
└── test-classes
├── SomethingAcceptanceTests.class
├── SomethingIntegrationTests.class
├── SomethingTest.class
├── acceptance-resource.txt
├── integration-resource.txt
└── unit-resource.txt
Updated Maven pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>some.group</groupId>
<artifactId>some.artifact.id</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>some.group.some.artifact.id</name>
<properties>
<project.dir.test.unit.java>src/test/unit/java</project.dir.test.unit.java>
<project.dir.test.unit.resources>src/test/unit/resources</project.dir.test.unit.resources>
<project.dir.test.integration.java>src/test/integration/java</project.dir.test.integration.java>
<project.dir.test.integration.resources>src/test/integration/resources</project.dir.test.integration.resources>
<project.dir.test.acceptance.java>src/test/acceptance/java</project.dir.test.acceptance.java>
<project.dir.test.acceptance.resources>src/test/acceptance/resources</project.dir.test.acceptance.resources>
<mvn.plug.failsafe.version>2.17</mvn.plug.failsafe.version>
<mvn.plug.build-helper.version>1.5</mvn.plug.build-helper.version>
</properties>
<build>
<testSourceDirectory>${project.dir.test.unit.java}</testSourceDirectory>
<testResources>
<testResource>
<directory>${project.dir.test.unit.resources}</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${mvn.plug.failsafe.version}</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>acceptance-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*AcceptanceTests.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${mvn.plug.build-helper.version}</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.dir.test.acceptance.java}</source>
<source>${project.dir.test.integration.java}</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.dir.test.integration.resources}</directory>
</resource>
<resource>
<directory>${project.dir.test.acceptance.resources}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Summary
We looked at how we can segregate different types of tests within the project using maven-failsafe-plugin and build-helper-maven-plugin
No comments:
Post a Comment