Spring Boot 2.X 快速集成单元测试解析
作者:java_lover 时间:2023-11-11 02:45:05
一、实现原理
使用MockMvc发起请求,然后执行API中相应的代码,在执行的过程中使mock模拟底层数据的返回,最后结果验证。
二、常用注解介绍
@SpringBootTest是SpringBoot的一个用于测试的注解,通过SpringApplication在测试中创建ApplicationContext。
@AutoConfigureMockMvc是用于自动配置MockMvc。
@RunWith在JUnit中有很多个Runner,他们负责调用你的测试代码,每一个Runner都有各自的特殊功能,你要根据需要选择不同的Runner来运行你的测试代码。
@Before在每个测试方法前执行,一般用来初始化方法。
@After在每个测试方法后执行,在方法执行完成后要做的事情。
三、主要代码
引入测试jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
测试类中添加注解和测试代码
package com.example.helloSpringBoot;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {HelloSpringBootApplication.class})
@AutoConfigureMockMvc //测试接口用
public class HelloControllerTest {
private static final Logger log = LoggerFactory.getLogger(HelloControllerTest.class);
@Before
public void testBefore(){
log.info("测试前");
}
@After
public void testAfter(){
log.info("测试后");
}
@Autowired
private MockMvc mockMvc;
/**
* 测试 /mockTest
*
*
*/
@Test
public void mockTest()throws Exception{
MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/mockTest")).
andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
int status=mvcResult.getResponse().getStatus();
//打印出状态码,200就是成功
log.info("状态码="+status);
Assert.assertEquals(200,status);
}
}
运行mockTest
运行成功后截图如下:
上述三步操作完成后即可实现对API(Controller)测试,有问题欢迎留言沟通哦!
完整源码地址:https://github.com/suisui2019/helloSpringBoot
来源:https://www.cnblogs.com/haha12/p/10789738.html
标签:spring,boot,集成,单元,测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring MVC入门_动力节点Java学院整理
2023-11-03 20:35:31
![](https://img.aspxhome.com/file/2023/3/58753_0s.jpg)
Android编程之绘图canvas基本用法示例
2022-08-21 15:02:23
Spring创建Bean的生命周期详析
2022-01-27 06:33:26
![](https://img.aspxhome.com/file/2023/8/71178_0s.png)
Java 关于String字符串原理上的问题
2021-05-26 12:48:44
Qt for Android开发实例教程
2023-06-27 10:00:39
SpringBoot整合activemq的案例代码
2023-11-06 18:41:37
![](https://img.aspxhome.com/file/2023/9/58589_0s.png)
springmvc+shiro自定义过滤器的实现代码
2021-08-11 21:23:11
![](https://img.aspxhome.com/file/2023/9/73059_0s.png)
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
Java中switch的三种用法方式小结
2023-11-24 03:40:21
Windows同时安装两个版本JDK并实现动态切换JAVA8或JAVA11的方法
2022-05-20 05:38:41
![](https://img.aspxhome.com/file/2023/5/71945_0s.jpg)
Qt 使用QDialog实现界面遮罩的示例(蒙版)
2022-10-31 07:58:37
![](https://img.aspxhome.com/file/2023/6/109776_0s.jpg)
Android Spinner 组件的应用实例
2022-07-12 04:50:13
java使用Jdom实现xml文件写入操作实例
2022-09-30 02:38:23
C#实现创建,删除,查找,配置虚拟目录实例详解
2022-09-27 06:38:16
java多态的向上转型的概念及实例分析
2023-06-12 08:30:35
使用SpringMVC在redirect重定向的时候携带参数的问题
2021-06-17 05:18:43
java操作solr实现查询功能的实例
2023-08-04 10:41:34
![](https://img.aspxhome.com/file/2023/0/95940_0s.png)
从java源码分析线程池(池化技术)的实现原理
2021-12-24 00:01:24
![](https://img.aspxhome.com/file/2023/3/80933_0s.webp)
java实现顺序结构线性列表的函数代码
2023-01-28 13:49:31
java清除html转义字符
2023-03-18 12:20:10