springboot利用mock进行junit单元测试,测试controller
1 spring-boot-starter-test内置mockito,添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2 示例controller
package com.shangcg.controller; import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @version v1.0
* @Description: junit和mock单元测试示例
* @Author: shangcg
* @Date: 2019/12/24
*/ @RestController
public class UnitDemoController { @RequestMapping(value = "/hello.json", method = RequestMethod.GET)
public String getListTag(HttpServletRequest request,
@RequestParam(value = "name", required = false, defaultValue = "0") String name) {
try {
return "hello :" + name;
} catch (Exception e) {
e.printStackTrace();
}
return "hello everyone !";
} @RequestMapping(value = "/save.json", method = RequestMethod.POST)
public String saveTag(HttpServletRequest request,
@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "level", required = true) Integer level) {
try {
return "recive your param " + "name: " + name + " level: " + level;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3 示例测试类
package com.shangcg.controller; import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.http.MediaType;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; /**
* @version v1.0
* @Description: TODO
* @Author: shangcg
* @Date: 2019/12/24
*/ @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UnitDemoControllerTest { @Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc; @Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();//建议使用这种
} @Test //对get方法的测试
public void testGetListTag() throws Exception { MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/hello.json")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.param("name", "shangcg")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn(); String content = mvcResult.getResponse().getContentAsString();
Assert.assertEquals("hello :shangcg", content);
} @Test //对post测试
public void saveTag() throws Exception { MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/save.json")
.contentType(MediaType.APPLICATION_JSON)
.param("name", "shangcg")
.param("level", "1")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
String content = mvcResult.getResponse().getContentAsString();
Assert.assertEquals("recive your param name: shangcg level: 1", content);
}
}
4 返回结果

5 因示例项目代码较多没法上传,需要源码请留言
springboot利用mock进行junit单元测试,测试controller的更多相关文章
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- SpringBoot: 16.整合junit单元测试(转)
1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...
- java基础第11期——Stream流、方法引用、junit单元测试
1.Stream流 Stream流与io流是不同的东西,用于解决集合类库已有的弊端, 1.1 获取Stream流: Collection集合的Stream方法,注意Map集合要经过转化 default ...
- Spring MVC如何测试Controller(使用springmvc mock测试)
在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...
- spring boot(三)Junit 测试controller
Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法 一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期 ...
- ssm controller层 junit单元测试
原文链接:https://www.cnblogs.com/oucbl/p/5943743.html springmvc controller junit 测试 作者:blouc@qq.com本文为作者 ...
- springboot使用MockMvc测试controller
通常,在我们平时开发项目时,如果想要输入URL对Controller进行测试,在代码编辑之后,需要重启服务器,建立http client进行测试.这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不 ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
- mybatis-generator没有自动生成代码和Junit测试controller
本来mybatis的generator想要自动生成增删改的,但是到后来语句就两个select,原因是数据中没有给字段加primary,就不会有删改增. 以及Controller的Junit测试 先导入 ...
随机推荐
- 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 百篇博客分析OpenHarmony源码 | v67.01
百篇博客系列篇.本篇为: v67.xx 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 51.c.h.o 文件系统相关篇为: v62.xx 鸿蒙内核源码分析(文件概念篇) | 为什么说一 ...
- 鸿蒙内核源码分析(索引节点篇) | 谁是文件系统最重要的概念 | 百篇博客分析OpenHarmony源码 | v64.01
百篇博客系列篇.本篇为: v64.xx 鸿蒙内核源码分析(索引节点篇) | 谁是文件系统最重要的概念 | 51.c.h.o 文件系统相关篇为: v62.xx 鸿蒙内核源码分析(文件概念篇) | 为什么 ...
- 📝 没 2 年 React Native 开发经验,你都遇不到这些坑
如果你喜欢我的文章,希望点赞 收藏 评论 三连支持一下,谢谢你,这对我真的很重要! React Native 开发时,如果只是写些简单的页面,基本上按着官方文档 reactnative.dev就能写出 ...
- State Space Model Content
State Space Model 状态空间模型及其卡尔曼滤波技术 混合正态分布下的状态空间模型及其滤波
- 数据库MHA原理
一.数据库的高可用MHA (1):详细的步骤 1.master mysql宕机了,MHA manager :无法连接master 2.MHA在S1 S2找一个延迟最小的slave,确定为未来的mast ...
- UI BLOCK自定义枚举控件的宽度
三步: 1.修改PresentationStyle属性为Radio Box 2.修改NumberOfColumns属性为指定的宽度(显示字符的个数) 3.将PresentationStyle属性改回O ...
- git 更新与图形界面
git 软件更新:git update-git-for-windows 或者 git update gitk 是一个历史记录的图形化查看器. 使用:只需 cd 到一个 Git 仓库,然后键入:gitk ...
- 初始HTML04
HTML 列表标签 无序列表 默认用实心圆点标识列表项 1 <ul> 2 <li>list item 列表项</li> 3 <li>list item ...
- 《JavaScript DOM编程艺术》:innerHTML
来源:第七章 动态创建标记 innerHTML: 1.HTML页面建立空白div: <div id="testdiv"> </div> <script ...
- Less-(5~7) error based
Less-5: 核心语句: 我们注意到,当输入正确时,并不能获得有价值的回显.好在出现错误时,会爆出错误内容: 于是,使用报错注入: 1' and updatexml(1,concat(0x7e,( ...