spring controller 方法测试
controller 测试 不使用其他api接口测试工具
一般而言,我们写好一个模块后,会对其进行单元测试,再集成到现有的系统中。
但是呢~针对Controller、Service、Dao三层来说,我们最常的是对Service和Dao进行单元测试。然而Controller的测试,很多人还是启动Tomcat,使用Postman进行api测试,这样不仅需要等待很长的编译部署时间,而且无法逐个Controller功能进行单独测试,因此特意总结Controller层的单元测试。顺便说下,Postman挺好用的,只是我们仍然需要启动tomcat,有点浪费时间。
那么,我们需要有一种不需启动tomcat就可以测试controller层的方法。
接下来我们细细讲来。
controller所采用的是SpringMVC框架 。SpringMVC已经继承了单元测试的类
步骤:
1.创建普通类文件
2.引入Spring单元测试注释
@RunWith(SpringJUnit4ClassRunner.class) // 此处调用Spring单元测试类
@WebAppConfiguration // 调用javaWEB的组件,比如自动注入ServletContext Bean等等
@ContextConfiguration(locations = { "classpath:context.xml", "classpath:mvccontext.xml" }) // 加载Spring配置文件
public class TestPayTypeController {
@Autowired
PayTypeController payTypeController;//测试的controller类 @Autowired
ServletContext context; MockMvc mockMvc; @Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(payTypeController).build();
}
}
注意:
@WebAppConfiguration如果不加的话,是没法调用WEB的一些特性的。经常会被遗忘掉。。。
@ContextConfiguration中,需要把所有Spring的配置文件全部加载进来,因为有的项目中Spring 的xml配置是分拆的。 此处的xml是放在resources的根目录中。
3.引入spring注解后,Controller的单元测试需要模拟Server的运行,需要在class中进行WEB环境的初始化。
MockMvc是SpringMVC提供的Controller测试类
每次进行单元测试时,都是预先执行@Before中的setup方法,初始化PayTypeController单元测试环境。
4.前期准备工作都做好了。可以编写单元测试方法了。
先看get方法请求
ResultAction是用来模拟Browser发送FORM表单请求的。get()是请求的地址,accept()请求的内容 ;
@org.junit.Test // get请求
public void getListTest() throws Exception {
// 发送请求
ResultActions resultActions = this.mockMvc
.perform(MockMvcRequestBuilders.get("/paytype/list/all").accept(MediaType.APPLICATION_JSON));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("客户端获的数据:" + result);
}
在控制台打印如下,说明成功了!

接下来使用post 请求;
ResultAction是用来模拟Browser发送FORM表单请求的。post()是请求的地址,accept()请求的内容 param()请求的键值对,如果有多个参数可以后缀调用多个param();
MvcResult是获得服务器的Response内容。
@org.junit.Test // post请求
public void addTest() throws Exception {
// 发送请求
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/paytype/add")
.accept(MediaType.APPLICATION_JSON).param("typename", "一年停").param("payfee","4444.0").param("payto", "post"));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("客户端获的数据:" + result);
}
成功插入数据库;

一般常用的就get,post请求;至此,我们不需启动tomcat就能测试controller层的method。
附上controller类的method
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResultMessage add(PayTypeModel ptm) throws Exception {
ResultMessage result = new ResultMessage();
payTypeService.add(ptm);
result.setResult("Y");
result.setMessage("增加缴费类型成功");
return result; } @RequestMapping(value="/get",method=RequestMethod.GET)
public PayTypeModel get(@RequestParam int typeno) throws Exception
{
return payTypeService.get(typeno);
}
如有错误,欢迎留言指正!
spring controller 方法测试的更多相关文章
- Spring Controller单元测试
SpringMVC controller测试较简单,从功能角度划分,可分为两种.一种是调用请求路径测试,另一种是直接调用Controller方法测试. 调用请求路径测试 通过请求路径调用,请求需要经过 ...
- Spring Controller 获取请求参数的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...
- spring boot利用controller来测试写的类
我们在开发spring boot应用程序的时候,往往需要测试某个写好的类,但是在测试的时候发现不太好测试,用Junit等测试框架,总是会报一些问题,大致是找不到配置文件以及无法利用spring创建的对 ...
- Java Spring Controller 获取请求参数的几种方法
技术交流群:233513714 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...
- Spring MVC中 controller方法返回值
1.返回ModelAndView 定义ModelAndView对象并返回,对象中可添加model数据.指定view 2.返回String 1.表示返回逻辑视图名 model对象通过 model.add ...
- (转)Spring Boot(十二):Spring Boot 如何测试打包部署
http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- Spring Boot(十二):Spring Boot 如何测试打包部署
有很多网友会时不时的问我, Spring Boot 项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下 Spring Boot 如何开发.调试.打包到最后的投产上线. 开发阶段 ...
- Spring Boot从入门到放弃-Spring Boot 整合测试
站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...
随机推荐
- Harmonic Number (II)
Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...
- [spring jpa] 解决SimpleJpaRepository的多数据源配置问题
前言 前段时间使用spring jpa做了一个项目,由于涉及到了多个数据库,因此需要进行多数据源的配置.网上找了很多的资料,尝试着配置,都以失败告终.之后通过断点最终完成了多数据源的配置.这篇博客主要 ...
- [LeetCode]-011-Integer_to_Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- vue 中 event.stopPropagation() 和event.preventDefault() 使用
1.event.stopPropagation()方法 这是阻止事件的冒泡方法,不让事件向document上蔓延,但是默认事件任然会执行,当你掉用这个方法的时候,如果点击一个连接,这个连接仍然会被打开 ...
- 快速排序和二分查找(Go)
package main import ( "fmt" "sync") var waitGroup sync.WaitGroup func main() { d ...
- linux日常---2、lamp.sh安装lamp环境中的linux操作
linux日常---2.lamp.sh安装lamp环境中的linux操作 一.总结 一句话总结: 学不如用,学一百遍还不如真正多用几遍的来的效果好 1.linux下查看进程命令? ps 常用 ps - ...
- postgres 数据导入导出
# 导出表结构 pg_dump -U postgres -s helloworld > hello.sql # 导出表数据 pg_dump -U postgres helloworld & ...
- python - from … import …
from . import XXX 默认的就是在当前程序所在文件夹里__init__.py程序中导入XXX from .A import XXX 如果当前程序所在文件夹里没有 ...
- 通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
目录 文章目录 目录 Provisioned Space & Used Space Provisioned Space 的计算方式 Uncommitted Space 扩展:置备率的计算公式 ...
- 看看 Delphi XE2 为 VCL 提供的 14 种样式
看看 Delphi XE2 为 VCL 提供的 14 种样式 其实只提供了 13 个 vsf 样式文件, 还有默认的 Windows 样式, 共 14 种. 在空白窗体上添加 ListBox1 等控件 ...