Search code examples
mavenintellij-ideajunit5

Maven runs JUnit 5 tests but does not run TestFactory method


I have a number of tests in a project which I am able to run using Maven (mvn test), all of these are JUnit 5 using the org.junit.jupiter.api.Test annotation.

I have one other class which has a method using the org.junit.jupiter.api.TestFactory annotation. When I run the tests in IDEA all the tests run and the TestFactory is run as expected (it is used as a factory to generate a number of tests, each of which runs and then the results are produced).

When I run in Maven it says it is running the class but it doesn't run any methods within it and the test factory method code isn't run.

Here's some example code showing the import and how it is being used on the method.


import org.junit.jupiter.api.TestFactory;

...class definition here etc...

    @TestFactory
    public Collection<DynamicTest> runTests() throws Exception {
        System.out.println("[Test] TestFactory test running");

        ... other code here...

    }
   

Maven will say it is running the class but the printout above will not occur.

I've tried adding another empty test method which uses the @Test annotation but this also doesn't run.

If I have maven specifically run this test using -Dtest=xxx Maven will even print out that it is running the test but then show that no tests have been run:

Running tests.FactoryTest Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Update: changing the name of a method to start with 'test' means that Maven does run the method, and if I change that method to start with something else (e.g. 'run') maven does not run it. This is also a difference between IDEA and Maven it seems (IDEA will run whatever is marked as a @Test but Maven seems to require some sort of naming convention). But that said, altering the name of the @TestFactory method still doesn't get Maven to run it.

Is there a simple way to get maven to handle the depencies etc when running the tests but just use the same mechanism as IDEA or just use a more standard JUnit way to run the tests rather than applying its own interpretations?

Pom file as it relates to JUnit:

    <properties>
        <junit-platform.version>5.9.1</junit-platform.version>
    </properties>

    ...

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <type>maven-plugin</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>

mvn -v output:

Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /usr/local/Cellar/maven/3.8.5/libexec
Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

Solution

  • You have not configured Maven Surefire correctly.

    Thus, I assume that your tests are "running" using POJO Tests.

    See the official JUnit 5 - Maven sample application for a working example with JUnit Jupiter and Maven Surefire.