使用Maven无法执行Junit5测试

3
Maven执行
mvn clean test

我正在尝试在我的Maven项目中使用junit5,但是无法在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中正常工作。


你的项目结构是怎样的?你的单元测试在哪个目录中? - Robert Moskal
它们位于相应的src/test文件夹中。实际上,使用IntelliJ的快捷方式生成单元测试。会更新一下,确保使用junit4一切正常。 - Naman
1个回答

3
您应该按照以下方式配置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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接