In this article, we will learn and understand how categorically include or exclude specific test cases while running unit test using surefire plugin
1. Inclusions of unit test:
By default, surefire plugin will automatically pick all test cases with following wild-card (*) pattern
- **/Test*.java –> Java test filename starting with “Test” under directory “src/test/java”
- **/*Test.java –> Java test filename ending with “Test” under directory “src/test/java”
- **/*TestCase.java –> Java test filename ending with “TestCase” under directory “src/test/java”
If we don’t follow any of the above mentioned naming convention in our project, then specifically we can request maven to include certain java test files using surefire plugin in pom.xml (using <includes> tag of surefire plugin)
pom.xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>SpringExample.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
In above example, Java test filename “SpringExample.java” will be included to run unit test via surefire plugin
2. Exclusions of unit tests:
As we aware, surefire plugin by default run unit test automatically. We can explicitly turn off or disable running test cases while executing maven’s package or install command
But this configuration disables all unit test cases, so to exclude running certain test cases, we can use <excludes> tag under surefire plugin
Q) Why we need this ?
Certain test cases fails while building (package/install) project which causes entire project to BUILD FAILURE, in those cases we can suppress/exclude that particular test case to execute
pom.xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<excludes>
<exclude>**/TestBookService.java</exclude>
<exclude>**/SpringExampleTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
In above example, Java test filename “TestBookService.java” & “SpringExampleTest.java” will be excluded to run unit test via surefire plugin
Note: we can use regular expression to include or exclude unit test cases with pattern-matching
Useful Eclipse IDE shortcuts :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
Happy Coding !!
Happy Learning !!