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 方法测试的更多相关文章

  1. Spring Controller单元测试

    SpringMVC controller测试较简单,从功能角度划分,可分为两种.一种是调用请求路径测试,另一种是直接调用Controller方法测试. 调用请求路径测试 通过请求路径调用,请求需要经过 ...

  2. Spring Controller 获取请求参数的几种方法

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...

  3. spring boot利用controller来测试写的类

    我们在开发spring boot应用程序的时候,往往需要测试某个写好的类,但是在测试的时候发现不太好测试,用Junit等测试框架,总是会报一些问题,大致是找不到配置文件以及无法利用spring创建的对 ...

  4. Java Spring Controller 获取请求参数的几种方法

    技术交流群:233513714  1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...

  5. Spring MVC中 controller方法返回值

    1.返回ModelAndView 定义ModelAndView对象并返回,对象中可添加model数据.指定view 2.返回String 1.表示返回逻辑视图名 model对象通过 model.add ...

  6. (转)Spring Boot(十二):Spring Boot 如何测试打包部署

    http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...

  7. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  8. Spring Boot(十二):Spring Boot 如何测试打包部署

    有很多网友会时不时的问我, Spring Boot 项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下 Spring Boot 如何开发.调试.打包到最后的投产上线. 开发阶段 ...

  9. Spring Boot从入门到放弃-Spring Boot 整合测试

    站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...

随机推荐

  1. HDU2196computer

    就是求每个点为起始点的最长链的长度. 写一下各个数组的意思吧. f[i][0]为点i向下走最长的距离:f[i][1]为点i向下走第二长的距离: xia[i][0]为点i向下走最长距离所要走的儿子节点: ...

  2. 【BZOJ2521】 [Shoi2010]最小生成树

    Description Secsa最近对最小生成树问题特别感兴趣.他已经知道如果要去求出一个n个点.m条边的无向图的最小生成树有一个Krustal算法和另一个Prim的算法.另外,他还知道,某一个图可 ...

  3. mac 绑定阿里企业邮箱

    注意事项: 1. 收件服务器 千万得写对, 选 pop 就写 pop.mxhichina.com; 选 imap 就写 imap.mxhichina.com 2. 发件服务器 必须写,smtp.mxc ...

  4. 接口自动化request库入门

    requests库7个主要方法 r= requsts.get(),主要属性: r.raise_for_status()方法内部判断r.status_code是否等于200不需要增加额外的if语句,该语 ...

  5. [design pattern](7) Singleton

    前言 上面的章节中,我们介绍了工厂模式,它是创建型模式的一种.本章我们将会介绍 单例模式 ,它也是创建型模式的一种.单例模式是我们比较常用的一个设计模式,也是最简单的一种设计模式. 单例模式 介绍:确 ...

  6. IT界须知的故事——仙童八叛徒

    原文:http://blog.sina.com.cn/s/blog_457012450100vnbl.html 许多电脑史学家都认为,要想了解美国硅谷的发展史,就必须了解早期的仙童半导体公司.这家公司 ...

  7. PCA 最大方差理论的直观解释

    PCA 这个名字看起来比较玄乎,其实就是给数据换一个坐标系,然后非常生硬地去掉一些方差很小的坐标轴. 例:三维空间中,有一些数据只分布在一个平面上,我们通过"坐标系旋转变换",使得 ...

  8. IDEA 中常用快捷键的使用

    IDEA 中快捷键的使用 1:知道类名全局查找类:   Ctrl+Shift+Alt+N;      全局搜索: Ctrl+Shift+R 2:快速定位到类的文件夹: 3:  优化导入的类和包 (删除 ...

  9. EasyHook Creating a remote file monitor

    In this tutorial we will create a remote file monitor using EasyHook. We will cover how to: 使用EasyHo ...

  10. leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...