当前位置:   article > 正文

随手记之JUnit 4和JUnit 5对比_junit4 junit5

junit4 junit5

一、Intellij IDEA版本和JUnit

首先确认Intellij IDEA版本,2017.3.5版本之前都不支持Junit5,2017.3.5版本开始就支持Junit5了。

2018.2.5版本之前,使用快捷键Alt + Insert,只能选择JUnit Test->JUnit 4,
在这里插入图片描述
2018.3.3版本,就可以选择JUnit Test->JUnit 5了。
在这里插入图片描述
在这里插入图片描述
2018.2.5使用JUnit 5需要按快捷键Ctrl + Shift + T,进行选择。
在这里插入图片描述
先介绍一下Intellij IDEA安装JUnit插件方法:
点击File,选择Settings,点击Plugins,输入JUnit,
在这里插入图片描述
IDEA自带的JUnit插件和JUnitGeneratorV2.0插件都要勾选上,若只勾选JUnit可能导致无法自动生成测试文件,
若只勾选JUnitGenerator V2.0可能导致生成的测试文件无法运行。

二、Spring Boot 版本和JUnit

Spring Boot 在 2.* 版本之后,默认只能使用JUnit5 来进行单元测试。如果你还想使用JUnit4 进行单元测试,需要手动配置,方法如下:
在pom.xml文件中添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

如果测试类的引入爆红,

import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 
  • 1
  • 2
  • 3

在这里插入图片描述
还需要手动添加JUnit4的依赖:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope><!-- 如果编译不通过,去掉这行即可 -->
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果配置文件是这样的:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
       <exclusion>
           <groupId>org.junit.vintage</groupId>
           <artifactId>junit-vintage-engine</artifactId>
       </exclusion>
    </exclusions>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

需要删除

<exclusions>
   <exclusion>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
   </exclusion>
</exclusions>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果使用JUnit5 来进行单元测试,在配置文件中添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
       <exclusion>
           <groupId>org.junit.vintage</groupId>
           <artifactId>junit-vintage-engine</artifactId>
       </exclusion>
    </exclusions>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

手动添加JUnit5的依赖:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.5.2</version>
    <scope>test</scope><!-- 如果编译不通过,去掉这行即可 -->
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

或者:

<!-- Junit 5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

注意:
junit-vintage-engine 和 junit-jupiter-engine 有什么不同
junit-vintage-engine 是 JUnit 4 中使用的测试引擎。
junit-jupiter-engine 是 JUnit 5 中使用的测试引擎。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/712203
推荐阅读
相关标签
  

闽ICP备14008679号