Maven not running JUnit 5 tests

删除回忆录丶 提交于 2019-12-01 17:05:09
LaurentG

According to the annotation (import org.junit.jupiter.api.Test), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency:

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

Your version of Maven comes with a version of maven-surefire-plugin which does not support JUnit 5. You could update your Maven to the latest version. You could also set the version of the maven-surefire-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
    <version>2.22.0</version>
</plugin>

See the junit5-samples for this information.

See the Maven Surefire Plugin artifact in a Maven repository. At version 3.0.0-M3 as of 2019-01.

junit-jupiter — the simpler archetype

The Answer by LaurentG seems to be correct, but a bit outdated.

As of JUnit 5.4, you can replace those multiple Maven artifacts:

  • junit
  • junit-jupiter-api
  • junit-jupiter-engine

…with a single artifact:

This new artifact is a aggregate of other artifacts, a convenient wrapper to simplify your POM file.

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.0-M1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

This gives you all you need to write and run JUnit 5 Jupiter tests.

If you have old JUnit 3 or JUnit 4 legacy tests that you want to continue to run, add a second dependency, junit-vintage-engine.

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
    <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.0-M1</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
    <!-- Enables any legacy JUnit 3 and JUnit 4 tests you may have. Not needed for JUnit 5 tests. -->
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.5.0-M1</version>
        <scope>test</scope>
    </dependency>

</dependencies>

You would also need the Surefire plugin as shown in that other Answer. Be sure to get the latest version, as Surefire has had some important fixes/enhancements recently. Currently at version 3.0.0-M3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!