项目结构如下

1 引入测试的 maven 依赖

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

2 编写测试类

//导包
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; //测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClientuseApplicationTests { @Test
public void contextLoads() { } }

说明:1)在测试类中可以注入需要的 Service 类,可以使用 springboot 的声明式注入。

如果测试接口可以使用 MockMvc 进行模拟

 /**
* 模拟mvc测试对象
*/
private MockMvc mockMvc;

具体详情可以搜索 MockMvc 使用方法。

2) 在测试类中需要引入额外的依赖时可以使用 <scope> 来选择适用范围。例如

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

springboot 项目单元测试的更多相关文章

  1. SpringBoot项目单元测试

    关于SpringBoot的单元测试,描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下单元测试类命名为*Test,前缀为要测试的类名 2. 使用mock方式单元测 ...

  2. SpringBoot项目单元测试不经过过滤器问题

    SpringBoot使用MockMvc:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-featu ...

  3. SpringBoot系列: 单元测试

    SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...

  4. IDEA中SpringBoot项目快速创建单元测试

    如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...

  5. 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解

    ========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...

  6. SpringBoot项目创建与单元测试

    前言   Spring Boot 设计之初就是为了用最少的配置,以最快的速度来启动和运行 Spring 项目.Spring Boot使用特定的配置来构建生产就绪型的项目. Hello World 可以 ...

  7. SpringBoot系列: 单元测试2

    之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...

  8. SpringBoot进行单元测试

    SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...

  9. ABAP和Java SpringBoot的单元测试

    ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...

随机推荐

  1. Qt,Qt/E,Qtopia Core, Qtopia之间的区别和联系

    转自:http://www.qtcn.org/bbs/read.php?tid=10373 关于Qt,Qt/E,Qtopia Core, Qtopia这些版本之间的区别和联系: Qt泛指Qt的所有桌面 ...

  2. angular中的 登录检查 和 过期Session清理

    angular利用ui-router进行登录检查 SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页. 监听ui-router的satte事件可以实现当state切换的时候检 ...

  3. 转:清理系统垃圾的BAT代码

    @echo off title @echo off color 2 echo. echo. echo 请不要关闭此窗口! echo. echo 开始清理垃圾文件,请稍等...... echo. ech ...

  4. auto_ptr,unique_ptr,shared_ptr,weak_ptr

    http://mojijs.com/2016/08/218129/index.html http://www.cnblogs.com/lanxuezaipiao/p/4132096.html

  5. MongoDB numa系列问题一:[initandlisten] connection refused because too many open connections:

    1:Mongod日志有很多这样的报错: [initandlisten] connection refused because too many open connections: 2:查看系统的限制 ...

  6. 朴素贝叶斯分类器(Naive Bayes)

    1. 贝叶斯定理 如果有两个事件,事件A和事件B.已知事件A发生的概率为p(A),事件B发生的概率为P(B),事件A发生的前提下.事件B发生的概率为p(B|A),事件B发生的前提下.事件A发生的概率为 ...

  7. ArchLinux 启动等待1分半的问题

    细致看,原来在等待一个磁盘分区,UUID是我的swap分区,在 /etc/fstab中能够看到 # /dev/nvme0n1p2 UUID=3c5c07fe-e4d8-4248-9820-7b9310 ...

  8. lvalue & rvalue

    https://www.youtube.com/watch?v=UTUdhjzws5g 作者:知乎用户链接:https://www.zhihu.com/question/50652989/answer ...

  9. python模块之lib2to3(py2转py3自动化工具)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之lib2to3(py2转py3自动化工具) #http://tieba.baidu.com ...

  10. 转 通过phpize为php在不重新编译php情况下安装模块openssl

    假定:php编译安装路径:/usr/local/php/apache编译安装路径:/usr/local/apache/php配置文件路径:/etc/php.iniphp安装源路径:/usr/sourc ...