就是这么简单(续)!使用 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 ...
随机推荐
- BestCoder Round #39
-------好久没更新博客了,发现还是需要不断总结才能进步,所以还是把最近打的一些比赛记录一下. T1:Delete (hdu 5210) 题目大意: 给出n个数,然后要删掉k个,要求剩下的数中 不 ...
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- Python开发入门与实战7-Django Form
7. Django Form 7.1. Form表单 Django带有一个form库,称为django.forms,这个库可以处理上一章提到的包括HTML表单的自动生成以及数据验证. 我们在inven ...
- 4、时间同步ntp服务的安装于配置(作为客户端的配置)
yum安装ntpd服务 .yum -y install ntp ntpdate (安装时间同步ntp服务) . vi /etc/ntp.conf (修改ntpd服务的配置文件) 3.修改配置文 ...
- Objective-C学习笔记-第一天(2)
Objective-C中的协议,相当于Java中的接口 参考:http://www.cnblogs.com/zzy0471/p/3894307.html 一个简单的协议遵循: PersonProtoc ...
- github使用心的
Git是一个分布式的版本控制系统,最初由LinusTorvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.包括Rubinius和Mer ...
- js实现时间控件
<html><head> <title>时间控件</title></head><body > <input name=&q ...
- samba 配置
sudo apt-get install samba sudo apt-get install kdenetwork-filesharing vi /etc/samba/smb.conf [Share ...
- PAT (Basic Level) Practise:1016. 部分A+B
[题目链接] 正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA. ...
- Map/Reduce 工作机制分析 --- 作业的执行流程
前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...