UT = Unit Tests IT = Integration Tests. All my Integration test classes are annotated with @Category(IntegrationTest.class)
My goal is:
mvn clean install
=> runs UT and not IT
mvn clean install
-DskipTests=true => no tests are executed
mvn clean deploy
=> runs UT and not IT
mvn clean test
=> runs UT and not IT
mvn clean verify
=> runs UT and IT
mvn clean integration-test
=> runs IT and UT are not executed
mvn clean install deploy
=> runs UT and not IT
pom properties:
<junit.version>4.12</junit.version> <surefire-plugin.version>2.18.1</surefire-plugin.version> <failsafe-plugin.version>2.18.1</failsafe-plugin.version>
Compiler:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin>
Unit Tests:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <excludedGroups>com.xpto.IntegrationTest</excludedGroups> </configuration> </plugin>
Integration Tests:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>${failsafe-plugin.version}</version> <configuration> <groups>com.xpto.IntegrationTest</groups> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*.class</include> </includes> </configuration> </execution> </executions> </plugin>
My results are:
mvn clean install
=> OK
mvn clean install
-DskipTests=true => OK
mvn clean deploy
=> runs UT and not IT
mvn clean test
=> OK
mvn clean verify
=> NOK ... only UT are executed but IT also needs to be executed
mvn clean integration-test
=> NOK ... UT are executed and should not and IT aren't executed but should be executed
mvn clean install deploy
=> OK
Separating things Usually unit tests are fast and even if you run them by mistake, you won't feel uncomfortable. Integration, contract and acceptance tests can take several minutes if the project is huge. That's why it makes sense to separate integration and unit tests.
The Maven build lifecycle now includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way.
Both the Unit Test and Integration Test are very important and useful. Neither of these two tests can be considered more important than the other. They should be performed rigorously, on time and should always be an integral part of the development process.
As mentioned earlier, unit testing involves testing individual pieces of code while integration testing involves testing modules of code to understand how they perform alone and how they interact with each other. Another difference between the two is the frequency with which each test should be executed.
The solution is this:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.19.1</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>${surefire.skip}</skip> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
This will let you control which is executed.
running UT and IT's:
mvn clean verify
running IT's but NOT UT's
mvn clean verify -Dsurefire.skip=true
running UT but not IT's:
mvn clean verify -DskipITs=true
You need to follow the naming conventions of the plugins which makes life easier.
Naming conventions for maven-surefire-plugin (unit tests). Naming conventions for maven-failsafe-plugin (integration tests).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With