首页 > 解决方案 > 关于设置 Maven 依赖项的 JUnit5 文档的说明

问题描述

JUnit5 页面要求在 StackOverflow 上发布澄清问题,所以在这里。

用户指南说 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,并且 JUnit Platform 是“启动测试框架的基础” 。所以 JUnit Platform 听起来很重要。

它还说“要了解哪些工件可供下载并包含在您的项目中,请参阅依赖元数据。” 当你去那里时,有关 JUnit 平台的信息,10.1.1。JUnit Platform, Group ID: org.junit.platform, Version: 1.6.2 列在首位。所以听起来你必须将它包含在你的依赖项中。

但是,用户指南也建议去Build Support。在那里,Maven 指令建议包含maven-surefire-pluginmaven-failsafe-plugin和。junit-jupiter-apijunit-jupiter-engine

用户指南还建议查看示例项目。在那里,Maven 示例仅包含 artifact junit-jupiter

所以我的问题是:遵循哪个建议?实际的指令是什么?

对于普通的最终用户,似乎最好的选择是遵循示例项目。但是,它显示为用户指南页面中的最后一个建议,并且似乎与之前的解释相矛盾。这会产生一些不必要的猜测,我建议简化用户指南的这一部分,以便更直接地切入主题。

标签: mavenjunitdocumentationjunit5

解决方案


您可以使用junit-jupiterwhich 是一个同时包含junit-jupiter-api和的模块junit-jupiter-engine

/**
 * Aggregates all JUnit Jupiter modules.
 *
 * @since 5.4
 */
module org.junit.jupiter {
    requires transitive org.junit.jupiter.api;
    requires transitive org.junit.jupiter.engine;
    requires transitive org.junit.jupiter.params;
}

这个 pom 似乎可以开始(在此处找到),链接自:

对于 Maven,请查看 junit5-jupiter-starter-maven 项目。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>junit5-jupiter-starter-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
        <junit.jupiter.version>5.6.2</junit.jupiter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

所以:

  1. 如上创建 pom.xml
  2. 在 src/test/java/org/example/MyExampleTest.java 中创建测试类:
package org.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class MyExampleTest {

  @Test
  void thisFailsTest() {
    fail(); //start here
  }

}

  1. 从终端执行mvn test
  2. 构建将像这样失败
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ junit5-jupiter-starter-maven ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.MyExampleTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.017 s <<< FAILURE! - in org.example.MyExampleTest
[ERROR] thisFailsTest  Time elapsed: 0.014 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: 
    at org.example.MyExampleTest.thisFailsTest(MyExampleTest.java:10)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   MyExampleTest.thisFailsTest:10
[INFO] 
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
  1. 删除 fail() 并开始一个有用的测试:)

推荐阅读