无法使用Maven执行Junit5测试 [英] Unable to execute Junit5 tests using Maven

查看:796
本文介绍了无法使用Maven执行Junit5测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven执行

mvn clean test

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

I am trying to use junit5 for one of my maven projects but not able to execute the unit tests during the test phase using -

<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无法使用Junit 5测试以将依赖项更新为-


Tried implementing the solution mentioned @ Surefire is not picking up Junit 5 tests to update the dependencies to -

<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执行测试?

Question - Is this custom provider no more supported or is there any solution to executing the tests using maven, junit5 and/or junit5-api currently?

注意 -使用JUnit-4可以很好地执行测试.

Note - The test execution was working fine with JUnit-4.

推荐答案

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

You should configure the maven-surefire-plugin like this:

<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工件,并且仅在依赖项"部分的测试范围内即可:

You only need to include the junit-jupiter-api artifact and only in test scope in your dependencies section:

<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 了解更多信息.

编辑 -来自相同的文档.

Edit - From the same docs.

为了让Maven Surefire完全运行任何测试,请使用TestEngine 实现必须添加到运行时类路径.

In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.

<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>

这篇关于无法使用Maven执行Junit5测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆