关于建立Maven依赖项的JUnit5文档的说明

问题描述

junit5页面要求澄清问题,以便发布在StackOverflow上。

User Guide说JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,并且JUnit Platform是“启动测试框架的基础”。因此,JUnit Platform听起来非常重要。

它还说:“要找出可以下载并包含在项目中的工件,请参阅Dependency Metadata。”当您转到那里时,请获取有关JUnit Platform 10.1.1的信息。 JUnit平台,组ID:org.junit.platform,版本:1.6.2首先列出。因此,听起来好像您必须将其包含在依赖项中。

但是,《用户指南》也建议转到Build Support。那里,Maven指令建议包含maven-surefire-pluginmaven-failsafe-pluginjunit-jupiter-apijunit-jupiter-engine

用户指南还建议调查Example Projects。那里,Maven示例仅包含工件junit-jupiter

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

对于普通的最终用户来说,最好的选择似乎是遵循示例项目。但是,它显示为“用户指南”页面中的最后建议,并且似乎与之前的解释相矛盾。这会造成一些不必要的猜测,我建议您精简《用户指南》的这一部分,以便更直接地进行讨论。

解决方法

您可以使用junit-jupiter,这是一个同时包含junit-jupiter-apijunit-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似乎可以启动(找到here):

对于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. 构建会像这样
  3. 失败
[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,Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
  1. 删除fail()并开始进行有用的测试:)