SpringBoot项目单元测试
前言:
关于SpringBoot的单元测试,描述一下三种单元测试的方式。
1、约定
单元测试代码写在src/test/java目录下,单元测试类命名为*Test,前缀为要测试的类名。
2.、使用mock方式单元测试
Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样。
测试开始之前需要建立测试环境,setup方法被@Before修饰。通过MockMvcBuilders工具,使用WebApplicationContext对象作为参数,创建一个MockMvc对象。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)//这里的Application是springboot的启动类名
@WebAppConfiguration
public class StyleControllerTest { @Autowired
private WebApplicationContext context; private MockMvc mockMvc; private ObjectMapper mapper = new ObjectMapper(); @Before
public void setupMockMvc() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
} @Test
public void testSend() throws Exception {
Long id =1l;
//调用接口,传入添加的用户参数
mockMvc.perform(MockMvcRequestBuilders.get("/style/listStyleById")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print()); } }
3、使用Feign方式单元测试
以下是Feign接口的单元测试示例,启动项目,可以测试本jar提供的服务,不启动服务,改为远程服务地址,可以测试远程jar提供的服务。其中
@EnableFeignClients(clients = UserControllerTest.UserServiceFeignClient.class)
类似我们实际应用调用相关服务一样。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = UserControllerTest.class)
@Import({ FeignAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
@EnableFeignClients(clients = UserControllerTest.UserServiceFeignClient.class)
public class UserControllerTest { @FeignClient(value = "loan-server", url = "http://localhost:9070/")
public interface UserServiceFeignClient extends UserServiceClient {
} @Autowired
private UserServiceFeignClient userServiceFeignClient; @Test
public void getUser() {
User user = userServiceFeignClient.getSDKUserById(1);
System.out.println(user);
}
}
4、使用Http Rest API 单元测试
使用RestTemplate发起GET或POST请求,其中@SpringBootTest这两行注释掉就不启动SpringBoot容器直接进行远程调用测试。
@RunWith(SpringJUnit4ClassRunner.class)
public class LoanControllerTest { private final static String url = "http://localhost:9070/"; private static RestTemplate restTemplate = new RestTemplate(); @Test
public void test(){
ResponseEntity<String> response = restTemplate.exchange(url + "/loan/getLoanById?id=1" ,
HttpMethod.GET,
new HttpEntity(null),
String.class);
System.out.println("result: " + response.getBody());
}
}
SpringBoot项目单元测试的更多相关文章
- springboot 项目单元测试
项目结构如下 1 引入测试的 maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...
- SpringBoot项目单元测试不经过过滤器问题
SpringBoot使用MockMvc:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-featu ...
- SpringBoot系列: 单元测试
SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...
- IDEA中SpringBoot项目快速创建单元测试
如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...
- 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解
========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...
- SpringBoot项目创建与单元测试
前言 Spring Boot 设计之初就是为了用最少的配置,以最快的速度来启动和运行 Spring 项目.Spring Boot使用特定的配置来构建生产就绪型的项目. Hello World 可以 ...
- SpringBoot系列: 单元测试2
之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...
- SpringBoot进行单元测试
SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...
- ABAP和Java SpringBoot的单元测试
ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...
随机推荐
- 『TensorFlow』卷积层、池化层详解
一.前向计算和反向传播数学过程讲解
- xml字符串,xml对象,数组之间的相互转化
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...
- 非阻塞IO发送http请求
import socket from urllib.parse import urlparse from selectors import DefaultSelector, EVENT_READ, E ...
- vscode setting.jsonxx
// Place your settings in this file to overwrite the default settings { "files.autoGuessEncodin ...
- learning makefile 定义命令包
- Mysql phpStudy升级Mysql版本,流产了怎么办?
网上有一些phpStudy升级mysql的方法,如: https://www.cnblogs.com/GreenForestQuan/p/6496431.html 很不错,我的电脑一次成功,但是同事的 ...
- tensoFlow之DNN文本分类
TensorFlow文本分类: 亲测可用:https://blog.csdn.net/u012052268/article/details/77862202 简单实例:https://www.leip ...
- virsh命令和虚拟机克隆
virsh 命令 virsh list //列出正在运行虚拟机 virsh list --all //列出所有虚拟机 virsh console sunhao-1 //进入名字为sunh ...
- java面向对象编程(五)--四大特征之抽象、封装
1.抽象 我们在前面去定义一个类时候,实际上就是把一类事物的共有的属性和行为提取出来,形成一个物理模型(模版).这种研究问题的方法称为抽象. 2.封装 封装就是把抽象出来的数据和对数据的操作封装在一起 ...
- python自动化运维os语法
得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() 递归查询指定路径下的所有文件和目录:os.walk() 函数 ...