【问题标题】:Unable to execute Junit5 tests using Maven [duplicate]无法使用 Maven 执行 Junit5 测试 [重复]
【发布时间】:2017-05-05 05:32:06
【问题描述】:

Maven 执行

mvn clean test

我正在尝试将 junit5 用于我的一个 maven 项目,但无法在 test 阶段使用 - 执行单元测试 -

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
</dependency>

我得到的输出是 -

[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ utils ---

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

Results :

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

尝试实现@Surefire is not picking up Junit 5 tests 提到的解决方案以将依赖项更新为 -

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
</dependency>
<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit5-api</artifactId>
    <version>5.0.0-ALPHA</version><!--https://mvnrepository.com/artifact/org.junit/junit5-api -->
    <scope>test</scope>
</dependency>

并将插件更新为 -

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>surefire-junit5</artifactId>
            <version>5.0.0-ALPHA</version><!--couldn't find this on the central though-->
        </dependency>
    </dependencies>
</plugin>

但输出保持不变。

问题 - 目前是否不再支持此自定义提供程序,或者是否有任何解决方案可以使用 mavenjunit5 和/或 junit5-api 执行测试?

注意 - 测试执行在 JUnit-4 上运行良好。

【问题讨论】:

  • 您的项目结构如何?您的单元测试在哪个目录中?
  • 它们位于相应的src/test 文件夹本身中。事实上,使用 intelliJ 的快捷方式生成单元测试。会更新junit4

标签: java maven unit-testing junit5


【解决方案1】:

您应该像这样配置 maven-surefire-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>

您只需要包含 junit-jupiter-api 工件并且仅在您的依赖项部分的测试范围内:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0-M3</version>
    <scope>test</scope>
</dependency>

请查看http://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven了解更多信息。

编辑 - 来自相同的文档。

为了让 Maven Surefire 运行任何测试,TestEngine 必须将实现添加到运行时类路径。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2018-12-10
    相关资源
    最近更新 更多