Monday 13 August 2018

Apache Maven Build Tool Learnings - Java

How to seperate unit and integration test?

- maven-surefire-plugin : designed to handle unit test(Junit)
- maven-failsafe-plugin : designed to handle integration tests


           Reference Blog for complete information
           How to separate integration test from unit test




Following is the process to run Unit and Integration Tests separately

1) Under properties section of the pom.xml file, define following properties
<skipTests>false</skipTests>
<skipITs>${skipTests}</skipITs>
<skipUTs>${skipTests}</skipUTs>

2) Under build section add maven-failsafe-plugin(for integration tests) and maven-surefire-plugin(for unit tests) as follows

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

<skipTests>${skipTests}</skipTests>
<skipITs>${skipITs}</skipITs>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
<skipTests>${skipUTs}</skipTests>
</configuration>
</plugin>

3) Below are the commands to skip/run the tests seperately

i) mvn install -DskipUTs : Skips Unit tests
ii)mvn install -DskipITs : Skips Integration tests
iii)mvn install -DskipTests : Skips both Unit and Integration Tests

4) We need to name the integration test as per maven-failsafe-plugin naming convensions for example, *IT.java




Following are the steps to skip tests by default but want the ability to re-enable tests from the command line in surefire plugin.

1) In the Properties section of the pom.xml need to add a property

<skipTests>true</skipTests>

2) Modify maven-surefire-plugin the plugin under build section as follows

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<configuration>

<trimStackTrace>false</trimStackTrace>

<includes>

<include>**/*Test.java</include>

</includes>

<excludes>

<exclude>**/*IntegrationTest.java</exclude>

<exclude>**/*IT.java</exclude>

</excludes>

<skipTests>${skipTests}</skipTests>

</configuration>

</plugin>

3) When we run , mvn clean install or mvn test, unit tests will not run by default

4) We can use "mvn install -DskipTests=false" to run the tests from the command line

No comments:

Post a Comment