mavenjunitjunit5maven-surefire-plugin

How to run tagged tests that are explicitly excluded in pom.xml?


This is part of my pom.xml:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.5.3</version>
  <configuration>
    <excludedGroups>slow</excludedGroups>
  </configuration>
</plugin>

I want my @Tag("slow") JUnit5 tests to be disabled by default. It works. Now, I want to enable these tests in command line. I do this:

mvn test -DexcludedGroups=

No effect. They still are not running.


Solution

  • As you noted, Maven Surefire Plugin allows you exclude groups using the excludedGroups property. Instead of hard-coding it in the plugin's configuration, you can give the property itself a default value, and then override it from the command line.

    i.e., the relevant parts of your pom.xml should look like this:

    <project>
        <properties>
            <!-- Default excludedGroups value -->
            <excludedGroups>slow</excludedGroups>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.5.3</version>
                    <!-- Don't specify excludedGroups here! -->
                </plugin>
            </plugins>
        </build>
    </project> 
    

    That way, if you just run the tests, the property will be applied:

    āžœ  mvn test
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------< org.example:samplejava >-----------------------
    [INFO] Building samplejava 1.0-SNAPSHOT
    [INFO]   from pom.xml
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- resources:3.3.1:resources (default-resources) @ samplejava ---
    [INFO] Copying 0 resource from src/main/resources to target/classes
    [INFO] 
    [INFO] --- compiler:3.13.0:compile (default-compile) @ samplejava ---
    [INFO] Nothing to compile - all classes are up to date.
    [INFO] 
    [INFO] --- resources:3.3.1:testResources (default-testResources) @ samplejava ---
    [INFO] skip non existing resourceDirectory /Users/mureinik/src/untracked/samplejava/src/test/resources
    [INFO] 
    [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ samplejava ---
    [INFO] Nothing to compile - all classes are up to date.
    [INFO] 
    [INFO] --- surefire:3.5.3:test (default-test) @ samplejava ---
    [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
    [INFO] 
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] 
    [INFO] Results:
    [INFO] 
    [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.684 s
    [INFO] Finished at: 2025-06-17T12:03:17+03:00
    [INFO] ------------------------------------------------------------------------
    

    But you can override the property with a -D flag:

    āžœ  mvn test -DexcludedGroups=
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------< org.example:samplejava >-----------------------
    [INFO] Building samplejava 1.0-SNAPSHOT
    [INFO]   from pom.xml
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- resources:3.3.1:resources (default-resources) @ samplejava ---
    [INFO] Copying 0 resource from src/main/resources to target/classes
    [INFO] 
    [INFO] --- compiler:3.13.0:compile (default-compile) @ samplejava ---
    [INFO] Nothing to compile - all classes are up to date.
    [INFO] 
    [INFO] --- resources:3.3.1:testResources (default-testResources) @ samplejava ---
    [INFO] skip non existing resourceDirectory /Users/mureinik/src/untracked/samplejava/src/test/resources
    [INFO] 
    [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ samplejava ---
    [INFO] Nothing to compile - all classes are up to date.
    [INFO] 
    [INFO] --- surefire:3.5.3:test (default-test) @ samplejava ---
    [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
    [INFO] 
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running org.example.MySlowTest
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 s -- in org.example.MySlowTest
    [INFO] 
    [INFO] Results:
    [INFO] 
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.706 s
    [INFO] Finished at: 2025-06-17T12:04:18+03:00
    [INFO] ------------------------------------------------------------------------