SpringBoot整合JUnit5

  • 前言
  • 引入依赖
  • 常用注解
  • @DisplayName
  • @BeforeEach
  • @AfterEach
  • @BeforeAll
  • @AfterAll
  • @Timeout
  • @Disabled
  • @ExtendWith
  • @RepeatedTest
  • 总结

前言

JUnit5是Java的测试框架,可以帮助Java程序员对自己编写的代码进行测试,使用起来比较友好。借用JUnit5官网上一句话来说:The 5th major version of the programmer-friendly testing framework for Java and the JVM
本次就来简单总结下JUnit5的基本用法
链接: 官网地址

引入依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

常用注解

● @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
● @RepeatedTest :表示方法可重复执行
● @DisplayName :为测试类或者测试方法设置展示名称
● @BeforeEach :表示在每个单元测试之前执行
● @AfterEach :表示在每个单元测试之后执行
● @BeforeAll :表示在所有单元测试之前执行
● @AfterAll :表示在所有单元测试之后执行
● @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
● @Timeout :表示测试方法运行如果超过了指定时间将会返回错误
● @ExtendWith :为测试类或测试方法提供扩展类引用

@DisplayName

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}
}

运行结果

@BeforeEach

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}
}

运行结果

@AfterEach

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}@AfterEachvoid testAfterEach() {System.out.println("测试已经结束了。。。");}
}

运行结果

@BeforeAll

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@Test@DisplayName("第二次测试")void secondTest() {System.out.println(2);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}@AfterEachvoid testAfterEach() {System.out.println("测试已经结束了。。。");}@BeforeAllstatic void testBeforeAll() {System.out.println("所有的开始测试了。。。");}
}

运行结果

@AfterAll

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@Test@DisplayName("第二次测试")void secondTest() {System.out.println(2);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}@AfterEachvoid testAfterEach() {System.out.println("测试已经结束了。。。");}@BeforeAllstatic void testBeforeAll() {System.out.println("所有的开始测试了。。。");}@AfterAllstatic void testAfterAll() {System.out.println("所有的测试已经结束了...");}
}

运行结果

@Timeout

package com.gavin.boot;import org.junit.jupiter.api.*;import java.util.concurrent.TimeUnit;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@Test@DisplayName("第二次测试")void secondTest() {System.out.println(2);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}@AfterEachvoid testAfterEach() {System.out.println("测试已经结束了。。。");}@BeforeAllstatic void testBeforeAll() {System.out.println("所有的开始测试了。。。");}@AfterAllstatic void testAfterAll() {System.out.println("所有的测试已经结束了...");}@Test@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)void testTimeout() throws InterruptedException {Thread.sleep(600);}
}

运行结果

可以看出,如果方法执行的时间超过测试设置的时间就会报错

@Disabled

package com.gavin.boot;import org.junit.jupiter.api.*;import java.util.concurrent.TimeUnit;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);}@Test@DisplayName("第二次测试")void secondTest() {System.out.println(2);}@BeforeEachvoid testBeforeEach() {System.out.println("开始测试了。。。");}@AfterEachvoid testAfterEach() {System.out.println("测试已经结束了。。。");}@BeforeAllstatic void testBeforeAll() {System.out.println("所有的开始测试了。。。");}@AfterAllstatic void testAfterAll() {System.out.println("所有的测试已经结束了...");}@Test@Disabled@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)void testTimeout() throws InterruptedException {Thread.sleep(600);}
}

运行结果

可以看出,testTimeout()方法加上了@Disabled这个注解后,这个方法就没有执行了,就不会有报错了

@ExtendWith

如果我们想使用RedisTemplate,我就在类中注入RedisTemplate

package com.gavin.boot;import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Autowiredprivate RedisTemplate redisTemplate;@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);System.out.println(redisTemplate);}
}

运行结果

可以看出,RedisTemplate 并没有被注入到容器中,如果需要主要到spring容器中,就需要添加@SpringBootTest

package com.gavin.boot;import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest
@DisplayName("JUnit5功能测试类")
public class Junit5 {@Autowiredprivate RedisTemplate redisTemplate;@Test@DisplayName("第一次测试")void firstTest() {System.out.println(1);System.out.println(redisTemplate);}
}

运行结果

可以看出,RedisTemplate已经被注入到spring容器中
那为什么加了@SpringBootTest注解RedisTemplate就可以注入到spring容器中呢?我们点进@SpringBootTest注解看一下源码就知道了

可以看出,源码中也是添加了@ExtendWith注解,才能把@Autowired的类注入到spring容器中,这就是@ExtendWith注解的基本用法了

@RepeatedTest

package com.gavin.boot;import org.junit.jupiter.api.*;@DisplayName("JUnit5功能测试类")
public class Junit5 {@Test@RepeatedTest(value = 5)@DisplayName("第一次测试")void firstTest() {System.out.println(1);}
}

运行结果

可以看出方法运行了5次

总结

以上就是JUnit5中基本注解的用法,其它注解以后有时间再继续更新。

SpringBoot整合JUnit5相关推荐

  1. 在springboot整合mybatis遇到的数据库连接不上问题解决

    我用的: jdk1.8 mysql5.1版本 运行出现下列错误 org.mybatis.spring.MyBatisSystemException: nested exception is org.a ...

  2. SpringBoot整合的Mybatis出现的问题:org.springframework.beans.factory.NoSuchBeanDefinitionException:

    在学习springboot整合Mybatis时,遇到的一个错误,跟着老师的步伐敲 结果还是出现了一系列的问题 自己搜了三四个小时都找不出问题(本人比较钻牛角尖!!!...) 出现的报错内容 org.s ...

  3. springboot整合单元测试

    文章目录 单元测试 Junit5测试 1.JUnit5 的变化 2.JUnit5常用注解 3.断言(assertions) 1.简单断言 2.数组断言 3.组合断言 4.异常断言 5.超时断言 6.快 ...

  4. SpringBoot整合es提示错误:ElasticsearchException[Invalid or missing build flavor [oss]]

    文章目录 解析 修改版本 错误详情 SpringBoot整合es提示错误:ElasticsearchException[Invalid or missing build flavor [oss]] 解 ...

  5. SpringBoot第九篇: springboot整合Redis

    这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...

  6. es springboot 不设置id_原创 | 一篇解决Springboot 整合 Elasticsearch

    ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务. ElasticSearch是一个基于Lucene的 ...

  7. springboot整合shiro使用shiro-spring-boot-web-starter

    此文章仅仅说明在springboot整合shiro时的一些坑,并不是教程 增加依赖 <!-- 集成shiro依赖 --> <dependency><groupId> ...

  8. db2 springboot 整合_springboot的yml配置文件通过db2的方式整合mysql的教程

    springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件 先配置MySQL,代码如下 spring: datasour ...

  9. 九、springboot整合rabbitMQ

    springboot整合rabbitMQ 简介 rabbitMQ是部署最广泛的开源消息代理. rabbitMQ轻量级,易于在内部和云中部署. 它支持多种消息传递协议. RabbitMQ可以部署在分布式 ...

最新文章

  1. 二进制_Kubernetes集群二进制部署
  2. GeoAnalyticsServer在Linux下集群部署手册
  3. lwip之数据收发流程_3
  4. retrofit 解析百度地图api 返回数据_阿里二面:关于 Retrofit 你知道多少?看完你的offer稳了
  5. codeforces 269B Greenhouse Effect
  6. 一文直击Graph Embedding图表示学习的原理及应用
  7. php里的%是什么意思,大家帮小弟我看下这段代码中的“%”是什么意思
  8. Almost Sorted Array HDU - 5532
  9. 二层网络和三层网络的区别
  10. ActiveRecord模式整理
  11. python蟒蛇绘制步骤_如何用python绘制蟒蛇移动的样子-百度经验
  12. Spring MVC对象转换说明
  13. BOSS直聘发起“逆行者先行”招聘专场:优先录取抗疫志愿者
  14. jQuery Mobile中按钮button的data-*选项
  15. ubuntu如何解压.tar.gz
  16. weex 项目开发(五)自定义 过滤函数 和 混合 及 自定义 Header 组件
  17. 快递鸟批量打印电子面单接口及控件安装
  18. echarts实现水波球
  19. 计算机发展史 文档,计算机发展史课件
  20. 考研英语 单词常见熟词生义

热门文章

  1. 【GlobalMapper精品教程】036:基于DEM的流域计算生成流域图
  2. 使用cubemx工具的STM32对外部flash(W25Q64)的简单编程
  3. 安卓 linux服务器文件夹,安卓手机目录各个文件夹
  4. MATLAB模糊逻辑工具箱函数
  5. 记录千万级数据库去重问题
  6. Matlab之绘图工具箱大全
  7. mysql数据库的事务 acid 隔离级别 脏读 脏写 幻读 不可重复读
  8. 并联下垂控制(DROOP控制)_SIMULINK模型搭建详解
  9. ResourceAccessException: I/O error on POST request for
  10. CentOS的DNS服务器配置文件/etc/resolv.conf重置问题