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 ...
随机推荐
- BZOJ 3168 Luogu P4100 [HEOI2013]钙铁锌硒维生素 (矩阵求逆、二分图匹配)
线性代数+图论好题. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3168 (luogu) https://www.lu ...
- sql中left join、right join、inner join的区别
转自https://www.cnblogs.com/pcjim/articles/799302.html left join.right join.inner join区别 left join(左联 ...
- centos网卡配置NM_CONTROLLED=”yes” 慎用
今天有1台服务器修改了 /etc/sysconfig/network-scripts/ifcfg-eth0 配置文件中的一个参数: NM_CONTROLLED=“yes” 修改成 NM_CONTROL ...
- sqli-labs29-31关Background-6 服务器(两层)架构
首先介绍一下29,30,31这三关的基本情况: 服务器端有两个部分:第一部分为tomcat为引擎的jsp型服务器,第二部分为apache为引擎的php服务器,真正提供web服务的是php服务器.工作流 ...
- 阿里知识储备之二——junit学习以及android单元测试
一,junit框架 http://blog.csdn.net/afeilxc/article/details/6218908 详细见这篇博客 juit目前已经可以和maven项目进行集成和测试,而且貌 ...
- mysqli使用localhost问题
<?php $mysqli = new mysqli('localhost', 'root', '123456', 'mysql'); if ($mysqli->connect_error ...
- xml文件中 xmlns xmlns:xsi 等解释
http://bbs.csdn.NET/topics/390751819 maven 的 pom.xml 开头是下面这样的 <project xmlns="http://maven.a ...
- Qt数据库之数据库连接池
前面的章节里,我们使用了下面的函数创建和取得数据库连接: void createConnectionByName(const QString &connectionName) { QSql ...
- JNI写本地日志文件
调试JNI库 我喜欢反编译APK 然后替换.so文件 然后再编译成APK 其中写日志的话 用fopen("/sdcard/lei.txt","wb+")
- C++ 左值与右值
https://baike.baidu.com/item/%E5%B7%A6%E5%80%BC%E4%B8%8E%E5%8F%B3%E5%80%BC/5537417?fr=aladdin https: ...