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.

workerjoe
  • 2,421
  • 1
  • 26
  • 49
  • 1
    Use minimum maven-surefire-plugin 2.22.2 to things correctly working with JUnit Jupiter... – khmarbaise Aug 13 '19 at 13:47
  • 1
    Does it work with junit 4.12 ? Do your test classes end with a `Test` ? – Arnaud Claudel Aug 13 '19 at 14:15
  • 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. – workerjoe Aug 13 '19 at 14:26
  • Possible duplicate of [Surefire is not picking up Junit 5 tests](https://stackoverflow.com/questions/36970384/surefire-is-not-picking-up-junit-5-tests) – Lesiak Aug 13 '19 at 22:08
  • Lesiak and khmarbaise you both pointed me in the right direction. I needed to specify a version of the maven-surefire-plugin – workerjoe Aug 14 '19 at 11:31

1 Answers1

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>
workerjoe
  • 2,421
  • 1
  • 26
  • 49
  • 2
    Using this same declaration for `junit-jupiter` and `maven-surefire-plugin` still does not work when declaring `spring-boot-starter-parent` as `` – manasouza Aug 23 '19 at 01:19
  • @manasouza 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. – workerjoe Aug 23 '19 at 12:49
  • 1
    This is the project, if you want to try to replicate it: https://github.com/joeclark-phd/granite – workerjoe Aug 23 '19 at 12:49