2

I'm trying to figure this out for over a year now but I can't run JUnit5 suites unless I use the JUnit4 @RunWith annotation, which brings me some other issues. I'm trying to use the newer @Suite annotation instead but no matter what I try (name the test ending with IT, Test, run through IntelliJ, run through maven, include tag, exclude tag, etc), I keep getting the tests no found error. I think I'm really running out of ideas at this stage and would love someone to point it out what is it that I may still be missing.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>pact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.version>1.8</maven.compiler.version>
        <maven.target.version>1.8</maven.target.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-api</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>vcservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <testSourceDirectory>src/test/java/com/hmhco/vcservice</testSourceDirectory>
                    <trimStackTrace>false</trimStackTrace>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class GoogleTest {

    @Tag("google")
    @Test
    void testing() {
        System.out.println("testing google");
    }
}

Thank you very much.

Update

Added some more dependencies, which failure by failure have now brought me to this new error and updated pom file.

import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@SelectPackages("com.my.package")
@IncludeTags("google")
@Suite
public class TestSuiteGoogle {
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>pact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.version>1.8</maven.compiler.version>
        <maven.target.version>1.8</maven.target.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
        <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-api</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-engine</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.7.0</version>
        </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.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
          <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.7.0</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>vcservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <testSourceDirectory>src/test/java/com/hmhco/vcservice</testSourceDirectory>
                    <trimStackTrace>false</trimStackTrace>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-logger-api</artifactId>
                        <version>2.21.0</version>
                        <!-- to get around bug https://github.com/junit-team/junit5/issues/1367 -->
                        <scope>test</scope>
                        <optional>true</optional>
                    </dependency>
                </dependencies>

            </plugin>
        </plugins>
    </build>

</project>

enter image description here

enter image description here

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
  • Have you tried to use junit-platform-suite-engine instead of junit-platform-suite-api? The engine is the executing component and brings junit-platform-suite-api as transitive dependency. – johanneslink Apr 13 '22 at 04:24
  • Thank you. I've tried that but then I got another error, keeping both of them also gives me another error. Then I tried downgrading JUnit version to 5.7.0 which also didn't work. Googling the new error and it may perhaps be something to do with the surefire-plugin, but I still fix it. I've updated the post with my current pom. – Francislainy Campos Apr 13 '22 at 05:29
  • 5.7.0 does not come with a suite-engine, so this cannot work. As for surefire, I'm not sure how well 3.0-M5 already supports junit platform. Try 2.22.2. – johanneslink Apr 13 '22 at 09:35
  • And this line: "5.0.0-M2" is definitely not what you want. – johanneslink Apr 13 '22 at 09:39
  • You also don't want a dependency on "junit-platform-surefire-provider" since this is from before native support by sure-fire plugin. – johanneslink Apr 13 '22 at 09:41
  • A good starting point is https://github.com/junit-team/junit5-samples/tree/main/junit5-jupiter-starter-maven and then add only dependencies to engines you need. There they use surefire 3.0.0-M6 by the way. – johanneslink Apr 13 '22 at 09:43
  • I've tried now with the sample code but can't get it to work there either, I'm afraid. https://github.com/francislainy/junit5-samples/blob/main/junit5-jupiter-starter-maven/src/test/java/com/example/project/TestSuiteGoogleTest.java Same no tests found error. – Francislainy Campos Apr 13 '22 at 12:19

1 Answers1

2

The Junit 5 User Guide states:

In addition to the junit-platform-suite-api and junit-platform-suite-engine artifacts, you need at least one other test engine and its dependencies on the classpath.

Your project does not include the junit-platform-suite-engine. Please add the following dependency to your project:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-engine</artifactId>
            <scope>test</scope>
        </dependency>
JKomoroski
  • 23
  • 2
  • Thanks for your answer. I've done that and tried a few different commands but I still get the tests not found error. Some of them were `mvn -Dtest=com.example.project.TestSuiteGoogle test` `mvn -Dtest=com.example.project.TestSuiteGoogle integration-test` and I changed the testsuite class to scan the whole package rather than a specific class but unfortunately it didn't work. – Francislainy Campos Apr 27 '22 at 08:09
  • I've created a pull request that corrects your work so both maven and intellij work with your code. Take some time to read up on [Surefire](https://maven.apache.org/surefire/maven-surefire-plugin/index.html) and [Failsafe](https://maven.apache.org/surefire/maven-failsafe-plugin/index.html) as well as the suite documentation in the [Junit 5 User Guide section about suites](https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine). – JKomoroski Apr 27 '22 at 22:56