【问题标题】:Clarification on JUnit5 documention on setup of Maven dependencies关于设置 Maven 依赖项的 JUnit5 文档的说明
【发布时间】:2020-09-02 20:18:51
【问题描述】:

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

User Guide 表示 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,并且 JUnit Platform 是“启动测试框架的基础”。所以 JUnit Platform 听起来很重要。

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

不过,用户指南也建议转至Build Support。在那里,Maven 指令建议包含maven-surefire-pluginmaven-failsafe-pluginjunit-jupiter-apijunit-jupiter-engine

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

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

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

【问题讨论】:

    标签: maven junit documentation junit5


    【解决方案1】:

    您可以使用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. 构建会这样失败
    [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() 并开始一个有用的测试:)

    【讨论】:

    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多