Filter JUnit 5 test cases with the annotation @Tag("name_test") using Maven

Viewed 2889

I want to filter my Junit 5 test cases, I am using the annotation @Tag("type_test"). I run my test with maven with the command mvn -Dtests=a test but it runs all the cases. For example, I have two methods and I want run only the method with @Tag("a"), but the result in console is "Hello word 1" and "Hello word 2". See the example code.


    static Properties properties = null;

    @BeforeAll
        public static void setUp() throws Throwable {
        properties = CommonMethods.loadConfigPropertiesFile();
        RestAssured.baseURI = properties.getProperty("BASE_HOST");
    }

    @Test
    @Tag("a")
    public void test1() {
        System.out.println("hello word 1");
    }   


    @Test
    @Tag("b")
    public void test2() {
        System.out.println("hello word 2");
    }
}

pom.xml

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <includeTags>${tests}</includeTags>
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M2</version>
        </dependency>
    </dependencies>


3 Answers

The versions and libraries you're using are outdated. Please retry with:

  • Maven Surefire 2.22.1 (better: 3.0.0-M3)
  • JUnit Jupiter 5.3.2 (better: 5.4.0-M1)

See this sample pom.xml file that also covers how to filter tags:

<build>
    <plugins>
        <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <groups>a</groups>
                <!-- excludedGroups>slow</excludedGroups -->
            </configuration>
        </plugin>
    </plugins>
</build>

Source: https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml

For more details on how to configure the JUnit Platform with Maven see the JUnit 5 User Guide https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven or the Maven Surefire documentation: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

I found a solution. It important verify and try with the latest version of each dependency. In this example:

  • maven-surefire-plugin (3.0.0-M3)
  • junit-platform-surefire-provider (1.3.0-M1)
  • junit-jupiter-engine (5.4.0-M1)
  • junit-jupiter-api (5.4.0-M1)

Solutions

Without profiles:

pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <properties>
                    <includeTags>${tests}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
</dependencies>

And you can use the command mvn test -Dtests=a to execute only the methods with the annotation @Tag("a")

With profiles:

Add this example code in the pom.xml

<profiles>
    <profile>
        <id>serverdevelop</id>
        <properties>
            <tests>develop</tests>
        </properties>
    </profile>
    <profile>
        <id>servertesting</id>
        <properties>
            <tests>testing</tests>
        </properties>
    </profile>
    <profile>
        <id>serverproduction</id>
        <properties>
            <tests>production</tests>
        </properties>
    </profile>
</profiles>

And for example you can use the command mvn test -Pserverdevelop to execute only the methods with the annotation @Tag("develop"). This is very useful for separating test cases by environments.

No need to touch your POM file.

mvn surefire:test -D groups=a,b should also do the trick.

If you need to compile before running the tests, use:

mvn test -D groups=a,b

Note that the space put between -D and groups=a,b is optional and purely for cosmetic reasons.

Related