zoukankan      html  css  js  c++  java
  • 咱的Maven项目能用Junit5吗?

    1. 简介

    不同于 JUnit3 或者 JUnit4,JUnit 5 由多个模块组成,并三个核心的子项目:

    JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

    查看一下官网 go JUnit5,上面有最新版本显示:

    这版本号可以帮助我们填写下面的依赖 junit-platform-launcher AND junit-jupiter-engine AND junit-vintage-engine

    <dependencies>
    
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    为了 mvn test 顺利执行 Junit5 测试用例,那么你需要 Maven 插件 maven-surefire-plugin:

    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
    

    2. 思考

    这三个子项目,我们每次都需要引入吗?

    • 答案:不是的,实际还是需要因项目需求而异的。

    3. 应用场景

    首先项目必须 JDK8+

    JUnit 5 requires Java 8 (or higher) at runtime.

    • 也就是说需要 JDK8+ 的项目才能用 JUnit5,否则老老实实选用 JUnit4 吧。

    3.1 新项目用JUnit5

    如果,我们是一个新开的项目,需要使用 Junit5,怎么选?

    • 答案:只需要引入 junit-jupiter-engine 即可。
    <?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>
    
            <junit5.version>5.7.1</junit5.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit5.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>
    

    3.2 迁移至JUnit5

    如果,我们的项目是一个旧项目,用的是 JDK8 + JUnit4,现在新的测试用例想要用 JUnit5 来写,怎么办?

    • 答案:引入 junit-jupiter-engine AND junit-vintage-engine。原先的依赖 junit 可以考虑移除,因为 junit-vintage-engine 会为我们自动引入可传递依赖项 junit
    <?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-migration-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>
    
            <junit5.version>5.7.1</junit5.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit5.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>
    

    4. 解读

    要开始使用 JUnit5 Platform,您至少需要向项目中添加一个 TestEngine 实现。

    junit-jupiter-engine 项目包含实现类 JupiterTestEnginejunit-vintage-engine 项目包含实现类 VintageTestEngine

    如果您想用 Jupiter 编写测试,请将测试件 junit-jupiter-engine 添加到 POM 中的依赖项中。这将引入所有必需的依赖项。在这些依赖关系中,就有 junit-jupiter-api,它包含测试源代码编译所需的类和接口。

    我们通常使用的 @Test,@Disabled 等注解,Assertions 等断言 API 都出自 junit-jupiter-api

    我们在项目中引入依赖 junit-jupiter-engine 时,Maven 就会自动替我们引入 junit-jupiter-api

    如果您想通过JUnit平台编写和执行JUnit3或4测试,请将 junit-vintage-engine 添加到依赖项中,该引擎可传递地引入(并需要)junit:junit:4.12

    引入依赖 junit-vintage-engine 时,Maven 就会自动替我们引入 junit

    5. 为啥没用到 junit-platform-launcher?

    IntelliJ IDEA在IDEA 2017.3 之前发布了 JUnit 5 的特定捆绑包版本。因此,如果您想使用较新版本的Junit Jupiter,IDE中的测试执行可能会由于版本冲突而失败。在这种情况下,请按照下面的说明使用比 IntelliJ IDEA 捆绑的JUnit 5更新的版本。

    junit-platform-launcher 更多的是服务于 IDE 的,如果出现了版本冲突引起的测试执行失败,就需要添加,反之我觉得不怎么需要呢?

    因此,加或者不加这个 junit-platform-launcher 依赖都有一定的合理性。个人没有遇过这类问题,所以暂时不加。

    6. 参考文档

  • 相关阅读:
    Android文字跑马灯控件(文本自动滚动控件)
    Android中的“再按一次返回键退出程序”实现
    Android中 在显示ImageView时图片上面和下面都出现一段空白区间的解决办法
    问题解决The connection to adb is down, and a severe error has occured.
    android关于uses-permission权限列表
    菜鸟学习Andriod-弹窗
    Andriod使用webview控件往APP里内嵌网页
    Mysql初始化root密码和允许远程访问
    转:Vmware Exsi使用简要说明
    转:怎样在VMware ESXi上 克隆虚拟机
  • 原文地址:https://www.cnblogs.com/kendoziyu/p/14448425.html
Copyright © 2011-2022 走看看