Sirius
Sirius

Reputation: 39

Junit5 test suites with @Suite annotation doesn't execute tests with mvn test command

We intended to upgrade a JUnit4 based project to JUnit5. I modified JUnit4 suites according to the instruction in JUnit5 official site: https://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner-test-suite. Tests were not executed when using mvn test in command line: mvn test -Dtest=SuiteXXTest, neither in JUnit4 nor JUnit5 suite. Error msg: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5:test (default-test) on project XXX: No tests were executed! Yet both JUnit4 and JUnit5 suites can run and execute the included tests in IDEA. Maven surefire plugin version is 3.0.0-M5. Single test classes(ATest and BTest) are based on JUnit4 progamming model.

Old JUnit4 suite is like this:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import test.XX.*;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        ATest.class,
        BTest.class
})
public class SuiteXXTest {
}

Modified to:

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import test.XX.*;

@Suite
@SelectClasses({
            ATest.class,
            BTest.class
})
public class SuiteXXTest {

}

related dependencies:

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>1.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

Curiously, if I use the depracated @RunWith(JUnitPlatform.class) annotation, the test suites can execute properly. But according to JUnit5 official user guide, using this runner means tests will be executed in a JUnit4 envrionment, and I don't understand what condition is regarded as JUnit4 environment(JUnit5 dependency already configured in pom).

I don't known if it's a bug or my incorrect use.

Upvotes: 2

Views: 6126

Answers (3)

s4075983
s4075983

Reputation: 1

strange, I had the same problem, and when changing from surefire 3.1.2 back to 2.22.2 it worked, i.e. adds all test back to the test reports and does not generate individual tests.

Upvotes: 0

Sirius
Sirius

Reputation: 39

Found the solution here: https://github.com/junit-team/junit5/discussions/2753.

According to this answer I just changed my command from -Dtest=SuiteXXTest to -Dinclude=SuiteXXTest and it works fine. I think it has something to do with the surefire plugin. It seems surefire cannot pick up tests form suite annotated with @Suite when using -Dtest command.

Upvotes: 1

johanneslink
johanneslink

Reputation: 5341

RunWith is a JUnit 4 construct and won’t work with JUnit 5. Use JUnit Platform suites instead as described in https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine

Upvotes: 1

Related Questions