JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
其中
JUnit平台,其主要作用是在JVM上启动测试框架。它定义了一个抽象的TestEngineAPI来定义运行在平台上的测试框架,同时还支持通过命令行、Gradle和Maven来运行平台。
JUnit Jupiter,包含了JUnit5最新的编程模型和扩展机制。
JUnit Vintage,允许在平台上运行JUnit3和JUnit4的测试用例。
JUnit5对Java运行环境的最低要求是Java8,同时也兼容测试旧版本JDK编译出来的代码。

完整依赖:

<dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.5.2</version><scope>test</scope>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.5.2</version><scope>test</scope>
</dependency>
<dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><version>5.5.2</version><scope>test</scope>
</dependency>

2018年10月24日Maven 3.6.0发布,Maven才正式原生支持Junit5。在这个版本中,Maven团队一并发布了 Maven Surefire Plugin 2.22.0 和Maven Failsafe plugin 2.22.0,进而解决了对Junit5的支持问题。
在此之前,为了能在Maven中运行Junit5的测试用例,需要为 Maven Surefire plugin额外提供一个Junit5团队提供的Junit Provider。

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19.1</version><dependencies><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-surefire-provider</artifactId><version>1.1.0</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.1.0</version></dependency></dependencies>
</plugin>

如果将Maven升级到3.6.0及以上版本,那么junit-platform-surefire-provider这个依赖就不需要了。

// UserService类
@Service
public class UserServiceImpl implements UserService {@Overridepublic void printName() {System.out.println("UserServiceImpl");}
}

1.springboot2.2.0之前,spring-boot-starter-test默认支持的是junit4;
我这里的测试环境:spring boot2.1.0+maven3.5.4+jdk8+idea2019.1.3

   1.1junit4的测试:需要的的依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>测试类举例:@RunWith(SpringRunner.class)@SpringBootTest  // 如果启动报错,则需要指定启动类的classpublic class Springboot159ApplicationTests {@AutowiredUserService userService;@Testpublic void contextLoads() throws SQLException {userService.printName();}}1.2junit5的测试:需要的的依赖:<dependencies><!-- exclude junit 4 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>junit</groupId><artifactId>junit</artifactId></exclusion></exclusions></dependency><!-- junit 5 跟随spring-boot-dependencies版本 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>${junit-jupiter.version}</version><scope>test</scope></dependency><!-- 自已指定 这个版本还没有加入依赖管理 --><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.3.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.0</version></plugin></plugins></build>测试类举例:import com.ysy.HelloDemo;import com.ysy.service.UserService;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;/*** Created by Administrator on 2020/1/31 14:07*/@SpringBootTest(classes= HelloDemo.class)//@ExtendWith(SpringExtension.class)public class Test2 {@AutowiredUserService userService;@DisplayName("Test Spring @Autowired Integration")@Test //注意这里可以没有publicvoid testGet() {userService.printName();//assertEquals("UserServiceImpl", userService.printName());}}

2.springboot2.2.0之后,spring-boot-starter-test默认支持的是junit5;
我这里的测试环境:spring boot2.2.0+maven3.5.4+jdk8+idea2019.1.3

  2.1junit4的测试:需要的的依赖:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId></exclusion><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion><exclusion><groupId>org.mockito</groupId><artifactId>mockito-junit-jupiter</artifactId></exclusion></exclusions></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><!-- 不用指定版本<version>4.12</version> --></dependency>
</dependencies>测试类举例:import com.example.service.UserService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/*** Created by Administrator on 2020/1/31 15:03*/@SpringBootTest@RunWith(SpringRunner.class)public class Test2 {@AutowiredUserService userService;@Test  //注意 publicpublic  void contextLoads() {userService.printName();}}2.2junit5的测试:需要的的依赖:<dependencies><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></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>测试类举例:import com.example.service.UserService;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestpublic  class DemoYsy2ApplicationTests {@AutowiredUserService userService;@DisplayName("Test Spring @Autowired Integration")@Testvoid contextLoads() {userService.printName();}}

3.两个版本的依赖声明对比:

2.1.0的spring-boot-dependencies<junit.version>4.12</junit.version><junit-jupiter.version>5.3.1</junit-jupiter.version><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version>
</dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>${junit-jupiter.version}</version><type>pom</type><scope>import</scope>
</dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-junit-jupiter</artifactId><version>${mockito.version}</version>
</dependency><maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version><plugin><artifactId>maven-surefire-plugin</artifactId><version>${maven-surefire-plugin.version}</version></plugin>2.2.0的spring-boot-dependencies<junit.version>4.12</junit.version><junit-jupiter.version>5.5.2</junit-jupiter.version><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>${junit-jupiter.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-junit-jupiter</artifactId><version>${mockito.version}</version></dependency><plugin><artifactId>maven-surefire-plugin</artifactId><version>${maven-surefire-plugin.version}</version></plugin>

附加:
版本新特性:
2.1.0版本新特性:https://www.xttblog.com/?p=3299
2.2.0版本新特性:https://zhuanlan.zhihu.com/p/95545254
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes
junit5支持情况:https://cloud.tencent.com/developer/article/1522208

注意点:
1.Junit4中为org.junit.Test,而Junit5中为org.junit.jupiter.api.Test
2.maven3.6.3,截止目前(2020年1月31日17:05:24)
在IDEA使用会有点问题;建议使用之前的版本

后话:JUnit5和Mockito将是以后的单元测试标配;
上面只是简单的把依赖关系说明了一下,具体的测试使用还待学习。。
参考:
https://www.jianshu.com/p/0eb2dfea55b4

springboot使用junit5/junit4相关推荐

  1. SpringBoot整合JUnit5

    SpringBoot整合JUnit5 前言 引入依赖 常用注解 @DisplayName @BeforeEach @AfterEach @BeforeAll @AfterAll @Timeout @D ...

  2. SpringBoot单元测试Test Junit4和Junit5

    一.springboot2 中的 Junit5 使用 对于service层测试,Junit5中只需要@SpringBootTest即可,不需要额外Mockmvc Springboot 整合 Junit ...

  3. springboot项目使用junit4进行单元测试,maven项目使用junit4进行单元测试

    首先,maven项目中引入依赖 <dependency><groupId>junit</groupId><artifactId>junit</ar ...

  4. 单元测试框架怎么搭?快来看看新版Junit5的这些神奇之处吧!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 为什么使用JUnit5 JUnit4被广泛使用,但是许多场 ...

  5. SpringBoot脚手架工程快速搭建

    1.说明 本文汇总了搭建Spring Boot工程的文章, 可以用于快速搭建一个新的Spring Boot工程. 包括最开始的PowerDesigner数据库设计工具使用, 导出对应数据库的DDL脚本 ...

  6. SpringBoot--->>>单元测试-->>JUnit5的变化

    1.JUnit5 的变化 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库 作为最新版本的JUnit框架,JUnit5与之前版本的Junit框架有很大的不同.由三个 ...

  7. Spring Boot 集成 JUnit5,更优雅单元测试!

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:https://mp.weixin.qq.com/s/2Kdu-nYLF55Ui9A1-ybgFw 为什么使用JUnit5 J ...

  8. junit4和junit5_JUnit5 TestSuite替代

    junit4和junit5 JUnit4具有TestSuite类来聚合多个测试. 这在JUnit 5中不可用.通常,通过套件中的一堆命名测试进行的测试发现有些糟透了. 但是,如果目标不是测试发现,而是 ...

  9. springboot系列文章之使用单元测试

    前言 springboot提供了 spirng-boot-starter-test以供开发者使用单元测试,在引入 spring-boot-starter-test依赖后: <dependency ...

  10. springboot 之单元测试

    前言 springboot提供了 spirng-boot-starter-test以供开发者使用单元测试,在引入 spring-boot-starter-test依赖后: 1 2 3 4 5 < ...

最新文章

  1. python3 多重列表推导式
  2. JAVA不使用POI给Word文档添加水印
  3. 成功解决ImportError: cannot import name ‘Imputer‘
  4. ICG游戏:证明,先手不是必胜就是必败。
  5. Web前端期末大作业--绿色自适应医疗健康医院网页设计(HTML+CSS+JavaScript+)实现
  6. linux压缩和解压缩命令大全
  7. 优达学城深度学习之三(上)——卷积神经网络
  8. CPU亲和性(affinity)sched_setaffinity() 和 sched_getaffinity()
  9. You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUAGES setting
  10. 计算机发展史的内容概述,计算机及其发展史概述
  11. OCS2007R2升级LyncSrv2013 PART1:基础准备
  12. PHP控制网页过期时间的代码!
  13. 33.MySQL高可用架构
  14. vasp软件linux,QVASP 是一款 VASP 的辅助软件
  15. 微信公众号支付JSAPI
  16. 芯片的原理应用与分类
  17. 谁再说“游戏没用”,就拿这个回怼他!
  18. 《世界棒球》:日本职棒
  19. 计算机信息化教育的意义,论信息技术对于教育的重要性
  20. FPGA开发技巧备忘录——Xilinx JTAG to AXI Master IP的使用

热门文章

  1. iOS 开发技巧(一)
  2. 在Dialog中设置焦点失败?
  3. Attachments in Oracle Form
  4. java ssm旅游网站系统源码jsp maven项目推荐
  5. html遮罩层动画制作,《Flash遮罩层动画的制作》的教学反思
  6. ejb2.0详细开发过程
  7. 解决360Wifi无法在校园网和企业网下使用的方法
  8. ibm服务器查看刀片状态,IBM刀片服务器 blade center s常见问答
  9. 织梦采集侠自动采集伪原创发布设置
  10. asp.net在前台web页面中使用Javascript调用RTX腾讯通的聊天窗口