Unit Testing of Spring MVC Controllers1
我们的pom.xml文件相关的部分看起来如下:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
让我们找出我们如何利用Spring MVC框架Spring MVC控制器测试编写单元测试。
写控制方法的单元测试
每一个单元测试,我们写测试控制器方法的行为包括:
1、我们请求发送到测试控制器的方法
2、我们确认我们已经收到了预期的响应
Spring MVC 框架测试有几个“核心”类,我们可以用在我们的测试中执行这些步骤。这些描述如下:
我们可以利用的 MockMvcRequestBuilders类的静态方法建立我们的请求。或者更具体的说,我们可以
创建请求的建设者。然后作为方法的参数来执行实际的请求专递到方法。
该Mockmvc阶级是我们测试的主要入口点。我们可以通过调用它的执行请求(RequestBuilder requestbuilder)方法。
我们可以利用MockMvcResultMathers类的静态方法接收响应断言。
接下来我们看看一些例子,演示了如何我们可以在单元测试中使用这些类,我们将写下面的控制器方法的单元测试:
第一控制器的方法呈现的页面列出待办事项条目。
第二控制器的方法呈现了一个页面,显示一个单做输入信息。
第三控制器的方法处理这是用来添加新的待办事项记录到数据库的表单提交。
绘制TODO条目列表页
让我们以在控制器的方法用来渲染todo条目列表页面执行的外观开始。
想象的行为
该控制器的方法是用来显示todo信息化实施的步骤:
1、处理GET请求发送到URL“/”
2、他得到的todo通过调用接口的todoservice findall()方法。此方法返回一个列表的todo对象
3、它增加了接收列表模型
4、它返回渲染视图的名称
TodoController 类相关的部分看起来如下:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class TodoController{
private final TodoService service;
@RequestMapping(value="/",method=RequestMethod.GET)
public string findAll(Model model){
List<Todo> models=service.findAll();
model.addAttribute("todos",models);
return "todo/list";
}
}
现在我们已经准备好为这个方法编写一个单元测试。让我们来看看我们能做到。
测试:发现todo
我们可以写一个单元测试这个控制器的方法通过以下步骤:
1、创建测试数据,回来时,我们的服务调用的方法。我们使用的一个概念,称为测试数据生成器时,我们为我们的测试生成测试数据。
2、配置用于模拟对象返回创建的测试数据时,其findall()方法称为。
3、执行一个GET请求URL“/”。
4、确保HTTP状态码返回200。
5、确保返回的视图名称是“做/列表”。
6、确保请求转发到URL“/WEB-INF/JSP /待办事项列表。JSP”。
7、确保模型的属性称为:有两个项目。
8、确认模型的属性称为:包含正确的项目
9、验证我们的模拟对象的findall()方法被称为只有一次。
10、确保其他的模仿对象的方法没有在测试过程中被称为。
我们单位的源代码测试如下:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.Arrays;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppContext.class})
@WebAppConfiguration
public class TodoControllerTest {
private MockMvc mockMvc;
@Autowired
private TodoService todoServiceMock;
//Add WebApplicationContext field here
//The setUp() method is omitted.
@Test
public void findAll_ShouldAddTodoEntriesToModelAndRenderTodoListView() throws Exception {
Todo first = new TodoBuilder()
.id(1L)
.description("Lorem ipsum")
.title("Foo")
.build();
Todo second = new TodoBuilder()
.id(2L)
.description("Lorem ipsum")
.title("Bar")
.build();
when(todoServiceMock.findAll()).thenReturn(Arrays.asList(first, second));
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("todo/list"))
.andExpect(forwardedUrl("/WEB-INF/jsp/todo/list.jsp"))
.andExpect(model().attribute("todos", hasSize(2)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));
verify(todoServiceMock, times(1)).findAll();
verifyNoMoreInteractions(todoServiceMock);
}
}
渲染视图做首页
在我们可以为我们的控制器的方法编写单元测试,我们必须仔细看看这方法的实现。
让我们找到我们的控制器的实现。
控制器的方法,是用来显示一个待办事项输入信息是通过以下步骤:
1、它处理GET请求发送到URL /做/ {id}”。的{id}是一个路径变量,其中包含所请求的任务项的标识。
2、获得所要求的待办事项条目通过调用的todoservice接口findbyid()方法和通过所要求的做的条目ID作为方法参数。这个方法返回发现做入门。如果没有做的条目被发现,该方法抛出一个todonotfoundexception。
3、它增加了发现做进入模型。
4、它返回渲染视图的名称。
我们的控制器方法的源代码如下:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class TodoController {
private final TodoService service;
@RequestMapping(value = "/todo/{id}", method = RequestMethod.GET)
public String findById(@PathVariable("id") Long id, Model model) throws TodoNotFoundException {
Todo found = service.findById(id);
model.addAttribute("todo", found);
return "todo/view";
}
}
我们下一个问题是:
当一个todonotfoundexception扔?
在本教程的一部分,我们创建了一个异常解析器豆,用于处理我们的控制器类抛出的异常。该bean的配置如下:
@Bean
public SimpleMappingExceptionResolver exceptionResolver() {
SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
Properties exceptionMappings = new Properties();
exceptionMappings.put("net.petrikainulainen.spring.testmvc.todo.exception.TodoNotFoundException", "error/404");
exceptionMappings.put("java.lang.Exception", "error/error");
exceptionMappings.put("java.lang.RuntimeException", "error/error");
exceptionResolver.setExceptionMappings(exceptionMappings);
Properties statusCodes = new Properties();
statusCodes.put("error/404", "404");
statusCodes.put("error/error", "500");
exceptionResolver.setStatusCodes(statusCodes);
return exceptionResolver;
}
我们可以看到,如果一个todonotfoundexception扔,我们的应用使误差/ 404′视图并返回HTTP状态代码404。
很明显,我们写了这两个测试控制器的方法:
我们要写一个测试,确保我们的程序是正确工作时做的条目不发现。
我们要写一个测试验证了我们的程序是正确工作时做的条目被发现。
让我们看看如何写这些测试。
试验1:做的条目不发现
来自:http://java.dzone.com/articles/junit-testing-spring-mvc-1
Unit Testing of Spring MVC Controllers1的更多相关文章
- 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 ...
- Unit Testing of Spring MVC
试验1:做的条目不发现首先,我们必须确保我们的应用是工作性质所做条目不发现.我们可以写的测试以确保通过以下步骤: 1.配置的模拟对象时抛出一个todonotfoundexception findbyi ...
- Spring MVC Test -Controller
http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers- ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- [Mockito] Spring Unit Testing with Mockito
It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...
- MVC Unit Testing学习笔记
MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...
- 使用MockMvc测试Spring mvc Controller
概述 对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便 ...
- spring mvc 介绍
Spring MVC Tutorial tag. * * If you do not want to deal with the intricities of the noscript * secti ...
随机推荐
- 11 并发编程-(线程)-信号量&Event&定时器
1.信号量(本质也是一把锁)Semaphore模块 信号量也是一把锁,可以指定信号量为5,对比互斥锁同一时间只能有一个任务抢到锁去执行, 信号量同一时间可以有5个任务拿到锁去执行, 如果说互斥锁是合租 ...
- UI5-文档-4.27-Mock Server Configuration
我们只是在一个真实的服务上运行我们的应用程序,但是对于开发和测试我们的应用程序,我们不希望依赖于“真实”服务的可用性,或者在数据服务所在的系统上增加额外的负载. 这个系统就是所谓的后端系统,我们现在将 ...
- eclipse中创建多模块maven web项目
本文讲述在eclipse中创建分模块maven web项目. 暂时将一个项目分为controller:service:dao以及父类模块四部分. 1.创建父类模块. 创建一个简单的maven proj ...
- java public project default private
- <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)
application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...
- (动态规划)有 n 个学生站成一排,每个学生有一个能力值,从这 n 个学生中按照顺序选取kk 名学生,要求相邻两个学生的位置编号的差不超过 d,使得这 kk 个学生的能力值的乘积最大,返回最大的乘积
第2关:最强战队 挑战任务 绿盟和各大名企合作,举办编程能力大赛,需要选拔一支参赛队伍.队伍成员全部来自“绿盟杯”中表现优秀的同学,每个同学都根据在比赛中的表现被赋予了一个能力值.现在被召集的N个同学 ...
- vortex
vortex - Bing dictionary US['vɔr.teks]UK['vɔː(r)teks] n.旋涡:涡旋:低涡:感情(或局势)的旋涡 网络漩涡:涡流:旋风 变形Plural Form ...
- Passing the Message
Passing the Message http://acm.hdu.edu.cn/showproblem.php?pid=3410 Time Limit: 2000/1000 MS (Java/Ot ...
- 13-matlab图片转化
图片格式: 处理函数: rgb2gray() gray2rgb()
- 仿微信客户端 帧布局中加入fragment
学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1.默认显示第一个功能(微信 ...