Spring Boot新版本默认使用Junit5,pom依赖为:

<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>

同时,测试类的Demo如下,其中@SpringBootTest表示将该类作为Spring的测试类,加载到spring容器中,必不可少。

import static org.junit.jupiter.api.Assertions.assertEquals;

import example.util.Calculator;

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MyFirstJUnitJupiterTests {

    private final Calculator calculator = new Calculator();

    @Test
void addition() {
assertEquals(2, calculator.add(1, 1));
} }

如果项目中要使用旧版的Junit4,那么在pom文件中要删除掉“<exclusions>”这个对旧版本的支持,同时导入Junit4的相关依赖

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

在测试类中,不要忘了添加@Runwith(SpringRunner.class)注解,有时候@Test的导入的包也有影响,需要注意

文档:

Spring Boot:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/spring-boot-features.html#boot-features-testing

Junit5:https://junit.org/junit5/docs/current/user-guide/#writing-tests

最新文章

  1. SpringMVC参数自动绑定
  2. Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
  3. hdu 1863 - 畅通工程(MST)
  4. HTML学习笔记——块级标签、行级标签、图片标签
  5. css/js(工作中遇到的问题)
  6. CocoaPods Podfile 文件写法有讲究
  7. 08.C# System.Nulable&lt;T&gt;和空引用操作符(四章4.2-4.4)
  8. Java中单例设计模式总结
  9. Excel图表-&quot;DNA&quot;图
  10. 用CNN对CIFAR10进行分类(pytorch)
  11. SuperDiamond在JAVA项目中的三种应用方法实践总结
  12. JS判断元素 动画是否执行完成
  13. 网页全屏,modal 弹框无法显示的问题
  14. Sharding模式
  15. nginx 操作笔记
  16. Android-json解析:原生JSONObject+JSONArray的使用【转】
  17. ZW云推客即将登场
  18. bootstrap 4 panels已被card替换
  19. JAVA多线程提高三:线程范围内共享变量&amp;ThreadLocal
  20. Python+Selenium框架设计篇之-什么是POM

热门文章

  1. 二本非科班,秋招,实习,面试,offer之路
  2. Android多activity启动两种方式浅谈
  3. Vue——监听器watch
  4. [新手教程]申请https泛域名解析
  5. 【函数分享】每日PHP函数分享(2021-1-11)
  6. Docker-ce Centos8 笔记二:常见问题
  7. CPNDet:粗暴地给CenterNet加入two-stage精调,更快更强 | ECCV 2020
  8. Oracle获取session的IP方法
  9. C语言流程图画法(C语言学习笔记)
  10. Netty学习:伪共享