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

Spring Boot中进行Junit测试的更多相关文章

  1. Spring Boot中的测试

    文章目录 简介 添加maven依赖 Repository测试 Service测试 测试Controller @SpringBootTest的集成测试 Spring Boot中的测试 简介 本篇文章我们 ...

  2. 【Spring Boot&&Spring Cloud系列】Spring Boot中使用NoSql数据库Redis

    github地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/spring-boot-nosql-redis 一.加入依赖到 ...

  3. Spring Boot中使用Redis小结

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...

  4. Spring Boot 中使用 jpa

    本文原文版权归 CSDN Hgihness 所有,此处为转载+技术收藏,如有再转请自觉于篇头处标明原文作者及出处,这是大家对作者劳动成果的自觉尊重!! 作者:Hgihness 原文:http://bl ...

  5. spring boot 中logback多环境配置

    spring boot 配置logback spring boot自带了log打印功能,使用的是Commons logging 具体可以参考spring boot log 因此,我们只需要在resou ...

  6. Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具)

    Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具) 本文使用 Gradle 作为构建工具,关于 Gradle 构建工具,可以理解为是 Mav ...

  7. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  8. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  9. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. 访问需要HTTP Basic Authentication认证的资源的c#的实现

    string username="username"; string password="password"; //注意这里的格式哦,为 "usern ...

  2. HTML颜色

  3. mysql提权神器

    java -jar udf.jar 127.0.0.1 root 123456 [32/64]

  4. Elastisearch在kibana下常用命令总结

    1.获取所有数据 GET /_search 2.创建一个Document PUT /ecommerce/product/1 { "name" : "gaolujie ya ...

  5. hadoop完全分布式

    虚拟机克隆 a. vim /etc/udev/rules.d/70-persistent-net.rules 更改网卡名     b. vim /etc/sysconfig/network-scrip ...

  6. Github 简单使用

    第一步:打开官网:https://github.com 注册一个帐户. 第二步:创建仓库 填写仓库的名字和描述. 创建好了之后,点击"Branch master",创建分支--在文 ...

  7. Unity优化 1

    浅谈Unity中的GC以及优化(转) Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译文搬运了过来,作为上面思 ...

  8. 阿里面试官:什么是MySQL索引,为什么要有索引?

    一.什么是索引? 索引就好比字典的目录一样 我们通常都会先去目录查找关键偏旁或者字母再去查找 要比直接翻查字典查询要快很多 二.为什么要有索引? 然而我们在使用mysql数据库的时候也像字典一样有索引 ...

  9. React & Vue2 Butterfly图编排——让数据更自由地驱动DAG

    一.简介 Butterfly是由阿里云-数字产业产研部孵化出来的的图编辑器引擎,由咱们部门以及其他开发者共同维护开发,具有使用自由.定制性高的优势,已支持集团内外上百张画布,不夸张的说,我觉得可以算的 ...

  10. docker 删除和拉取镜像

    删除镜像 # docker rmi -f 镜像id # 删除指定镜像 docker rmi -f 25d5f6s564 # docker rmi -f 镜像id 镜像id # 删除多个镜像 docke ...