Search code examples
mavenjunitjunit5rest-assuredmaven-surefire-plugin

How to invoke JUnit5 test runner / Junit tag expressions using Maven Surefire plugin?


We're building a Modular framework to run API tests. Tests themselevs are annoted using JUnit / JUnit5 @Test annotations.

Option1:

  1. For running the tests together, I've created JUnit5 Test runner and tests are running fine using the runner locally in IntelliJ IDE

Option2:

  1. Also, alternative to above approach, we've started to add JUnit5 tags to categorise tests

A) When I try to call JUnit5 runner referred in above Option1, using Maven surefire plugin, no tests run.

B) Also when I try to run Tags using below approach inside Surefire plugin, expected tests doesn't run:

                 <properties>
                    <includeTags>SignUpTag,junit5Tag,ListingTag,dummy</includeTags>
                </properties>

                <!-- OR-->
                <groups>signUpTag,junit5Tag,listingTag,dummy,loginTag</groups>

Do you have any sample example on Git etc. about invoking JUnit5 test suite using Maven suirefire plugin?

Thanks a lot

enter image description here

enter image description here

POM.XML

<properties>
    <java.version>11</java.version>
    <maven.compiler.version>3.3</maven.compiler.version>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>
    <rest.assured.version>4.3.1</rest.assured.version>
    <testng.version>7.3.0</testng.version>
    <mvn.scala.version>2.15.2</mvn.scala.version>
    <jackson.databind.version>2.9.0</jackson.databind.version>
    <commons-io.version>2.1</commons-io.version>
    <wiremock.version>2.24.1</wiremock.version>
    <log4j.version>LATEST</log4j.version>
    <junit.jupiter.version>5.5.2</junit.jupiter.version>
    <junit.platform.version>1.5.2</junit.platform.version>
    <hamcrest.version>2.2</hamcrest.version>
    <tests>ListingTag</tests>
</properties>

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <tests>SignUpTag,junit5Tag,listingTag,dummy,loginTag</tests>
        </properties>
    </profile>
</profiles>

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>${rest.assured.version}</version>
    </dependency>

    <dependency>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>${mvn.scala.version}</version>
    </dependency>

    <!--        <dependency>-->
    <!--            <groupId>org.testng</groupId>-->
    <!--            <artifactId>testng</artifactId>-->
    <!--            <version>${testng.version}</version>-->
    <!--            <scope>test</scope>-->
    <!--        </dependency>-->

    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>${wiremock.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.databind.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons-io.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.0-alpha1</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.0.0-M3</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest</artifactId>
        <version>${hamcrest.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>27.0.1-jre</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.3</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <!--****************************** OPTION 1: ***************************-->
                <!--************************************************************************-->
                <includes>
                    <include>Junit5RunnerIT.java</include>
                    <include>**/Junit5RunnerIT*.java</include>
                    <include>**/*Junit5RunnerIT.java</include>
                </includes>
                <!--                    <testFailureIgnore>true</testFailureIgnore>-->
                <!--************************************************************************-->
                <!--****************************** OPTION 2: ***************************-->
                <!--************************************************************************-->
                <properties>
                    <includeTags>signUpTag,junit5Tag,ListingTag,dummy</includeTags>
                </properties>
                <!--                    <groups>junit5</groups>-->
                <!--                    <groups>${tests}</groups>-->
                <groups>signUpTag,junit5Tag,listingTag,dummy,loginTag</groups>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Solution

  • That is what I did, not sure it's the thing that you're looking for.

    pom.xml
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
            <groups>Demo1Test,Demo2Test,Demo3Test,Demo4Test</groups>
        </configuration>
    </plugin>
    

    Sample Test

    import org.junit.jupiter.api.Tag;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.TestInfo;
    
    @Tag("Demo1Test")
    public class Demo1Test {
    
        @Test
        void name1(TestInfo info) {
            System.out.println(info.getDisplayName());
        }
    }
    

    Run mvn clean test. This is the result:

    enter image description here