Maven silently fails to find JUnit tests to run
Asked Answered
I

1

2

I've got a new Java project open in IntelliJ with Maven as its build tool, with one class and one JUnit 5 test class at the moment. When I direct IntelliJ to run tests, individually or all together, it works. But when I go to the terminal and hit mvn clean test or do the same from the Maven pane within IntelliJ, it skips over the tests.

Unlike the questioner with this similar question, however, I'm not getting any error message. The test class is found and does compile. I do not have the same problem (incorrect file naming) that he had.

EDIT: Stackoverflow asks me why this isn't a duplicate of this question. It is the same problem, but their solution (from 2016) is no longer correct. You don't need to add the "provider" dependency anymore.

Here's the relevant section of my Maven output:

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ markovmodels ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\joe\foo\markovmodels\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ markovmodels ---
[INFO] Surefire report directory: C:\Users\joe\foo\markovmodels\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.648 s
[INFO] Finished at: 2019-08-13T09:02:53-04:00
[INFO] ------------------------------------------------------------------------

I don't know if it's a useful clue, but I observed that the target/surefire-reports directory was not created.

In the pom.xml I have these two test-related dependencies:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>

They are directly copied over from another project which works. I have not specified a version of the Surefire plugin or changed any of its defaults, so the effective POM is the same as my other projects (it uses maven-surefire-plugin version 2.12.4). The test source file seems to be in the right directory and have the right naming convention. What mistake could I be making?

The code at its current state can be here on Github.

Inkling answered 13/8, 2019 at 13:15 Comment(5)
Use minimum maven-surefire-plugin 2.22.2 to things correctly working with JUnit Jupiter...Pineal
Does it work with junit 4.12 ? Do your test classes end with a Test ?Lacefield
It wouldn't work with junit 4 because I use some of the new methods such as assertThrows. The test class ends with a @Nested class containing tests.Inkling
Possible duplicate of Surefire is not picking up Junit 5 testsPhototypy
Lesiak and khmarbaise you both pointed me in the right direction. I needed to specify a version of the maven-surefire-pluginInkling
I
4

It was an issue with the maven-surefire-plugin

There was something wrong with the default version of the maven-surefire-plugin, and I was able to fix it by upgrading that. I solved the problem by copying relevant sections from the JUnit5 sample Maven project on Github:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
Inkling answered 14/8, 2019 at 11:45 Comment(3)
Using this same declaration for junit-jupiter and maven-surefire-plugin still does not work when declaring spring-boot-starter-parent as <parent>Andrus
@Andrus Try adding the spring-boot-starter-test dependency as well. I have the two declarations above in one of my Spring Boot projects (plus a few other Spring Boot starters including spring-boot-starter-test) and it works. For now, anyway.Inkling
This is the project, if you want to try to replicate it: github.com/joeclark-phd/graniteInkling

© 2022 - 2024 — McMap. All rights reserved.