就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers
就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers
转载注明出处:http://www.cnblogs.com/wade-xu/p/4311205.html
接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MVC Controllers, 这个MockMvc 是建立在Spring MockMvc基础上的, 其目的是让我们用起来更便捷。
Getting Ready
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- Optional -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
Example
下面是我们要测试的Controller
package com.wadeshop.controller; import org.springframework.stereotype.Controller;
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.ResponseBody; @Controller
public class GreetingController { private static final String template = "Hello, %s!"; @RequestMapping(value = "/greeting", method = RequestMethod.GET)
@ResponseBody
public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
return new Greeting(String.format(template, name));
}
}
Greeting 类 如下
public class Greeting {
private final String content;
public String getContent() {
return content;
}
public Greeting(String content) {
this.content = content;
}
}
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4311205.html
接下来就是创建Spring MVC 测试类了
package com.wadeshop.controller; import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.Matchers.equalTo; import org.junit.Before;
import org.junit.Test; import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc; public class GreetingControllerTest { @Before
public void configured() {
RestAssuredMockMvc.standaloneSetup(new GreetingController());
} @Test
public void test1() {
given().
param("name", "Johan").
when().
get("/greeting").
then().
statusCode(200).
body("content", equalTo("Hello, Johan!"));
} @Test
public void test2() {
given().
param("name", "").
when().
get("/greeting").
then().
statusCode(200).
body("content", equalTo("Hello, World!"));
} }
单元测试过程无非就这些步骤:
1. 准备测试环境, 上面的例子就是使用 standalone setup 初始化MockMvc, 传入被测Controller
2. 传入参数构造请求并且调用
3. 验证结果
执行结果如下

是不是很简单?
这种方式其实就是纯粹的单元测试,如果想模拟真实的Spring MVC, 走Spring MVC完整流程,比如Dispatcher servlet, 类型转换,数据绑定等等, 则需要用MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 我在以后的文章中会介绍到。
参考
https://code.google.com/p/rest-assured/wiki/Usage#Spring_Mock_Mvc_Module
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4311205.html
就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers的更多相关文章
- 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers(转)
接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MV ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- 使用MockMvc测试Spring mvc Controller
概述 对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便 ...
- junit4测试 Spring MVC注解方式
本人使用的为junit4进行测试 spring-servlet.xml中使用的为注解扫描的方式 <?xml version="1.0" encoding="UTF- ...
- JUnit+Mockito结合测试Spring MVC Controller
[本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...
- Spring MVC(八)--控制器接受简单列表参数
有些场景下需要向后台传递一个数组,比如批量删除传多个ID的情况,可以使用数组传递,数组中的ID元素为简单类型,即基本类型. 现在我的测试场景是:要从数据库中查询minId<id<maxId ...
- 基于XML配置的Spring MVC 简单的HelloWorld实例应用
1.1 问题 使用Spring Web MVC构建helloworld Web应用案例. 1.2 方案 解决本案例的方案如下: 1. 创建Web工程,导入Spring Web MVC相关开发包. Sp ...
随机推荐
- 荣品RP4412开发板摄像头驱动调用及对焦控制
1.关于更换不同摄像头驱动调用问题. 问:RP4412开发板,我用的摄像头640*480图像预览时OK的,但是我调用1280*720的初始化预览,摄像头没有图像了,是不是camera程序也需要修改? ...
- NodeJs和ReactJs单元测试工具——Jest
Jest——Painless JavaScript UnitTesting 特点 适应性强 默认使用Jasmine断言 模块化的 可扩展的 可配置的 沙箱式且快速 虚拟化JS环境,模拟浏览器 并行运行 ...
- hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏
thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...
- CsvReader,CsvWriter的使用以及解决中文乱码
public void Csv(){ try { String[] stringList; String sourceFilePath = "D:\\111\\前海自身.csv"; ...
- 链式编程中的next()和end()
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- css2----清除浮动
为什么要清除浮动? 非IE下,当容器的高度为auto,容器有浮动元素,此时容器的高度不能自己伸长适应内容的高度,造成内容溢出乃至影响布局,即所谓的“浮动溢出”,为防此象,需要清除浮动. 如何清除浮动? ...
- DOM优化
一:DOM与浏览器: 重排:改变页面的内容. 重绘:浏览器显示的内容. 添加顺序:尽量在appendchild之前. 合并DOM操作-利用csstext, 缓存布局信息 文档碎片. 二 DOM 与事件 ...
- HDU5128 细心、细心、细心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给你n(n < 30)个点的坐标,然后让你求出这n个点能构成的两个最大矩形的面积,有 ...
- yii框架便利类CVarDumper使用
1.类文件位置:path/to/yiiframework/utils/CVarDumper.php 2.作用:CVarDumper is intended to replace the buggy P ...
- ajax 中$.each(json,function(index,item){ }); 中的2个参数表示什么意思?
$.each(json,function(index,item)里面的index代表当前循环到第几个索引,item表示遍历后的当前对象,比如json数据为:[{"name":&qu ...