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 ...
随机推荐
- [LightOJ1008]Fibsieve`s Fantabulous Birthday 题解
前言 扫了一眼网上的题解,都是找规律. 估计就我一个蒟蒻在打二分. 题解 设一个" ┐"形为一层. 我们二分查找该数在那一层,然后就可以直接计算它的位置了. 代码 #include ...
- ex1.c 补
表达式和符号运用生疏 经常少符号
- Spring Cloud Stream教程(二)主要概念
Spring Cloud Stream提供了一些简化了消息驱动的微服务应用程序编写的抽象和原语.本节概述了以下内容: Spring Cloud Stream的应用模型 Binder抽象 持续的发布 - ...
- D. Print a 1337-string...
D. Print a 1337-string... 输出一个字符串 里面包含n个子序列 1337 #include<bits/stdc++.h> using namespace std; ...
- PHP通用后台管理系统发布!
下载地址:https://gitee.com/lim2018/phpadmin
- linux日常---2、lamp.sh安装lamp环境中的linux操作
linux日常---2.lamp.sh安装lamp环境中的linux操作 一.总结 一句话总结: 学不如用,学一百遍还不如真正多用几遍的来的效果好 1.linux下查看进程命令? ps 常用 ps - ...
- Word2Vec模型参数 详解
用gensim函数库训练Word2Vec模型有很多配置参数.这里对gensim文档的Word2Vec函数的参数说明进行翻译,以便不时之需. class gensim.models.word2vec.W ...
- 四十三、jenkins启动时报错:consider increasing the maximum size of the cache. After eviction approximately [10,239] KB of data
jenkins启动时报错: consider increasing the maximum size of the cache. After eviction approximately [10,23 ...
- 四十、python中的生成器和迭代器
A.生成器(包含yield的就是生成器) def func(): print(11) yield 1 print(22) yield 2 print(33) yield 3 print(44) yie ...
- 【C++进阶】getline
在<istream>中的getline函数有两种重载形式: istream& getline (char* s, streamsize n );istream& getli ...