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 ...
随机推荐
- onMouseDown和onPress的差异AS2
為了要做出比Button物件更複雜的互動,我們通常會改用MovieClip來製作按鈕.如此一來,就需要處理event handler.與滑鼠有關的MovieClip event handler包括on ...
- Simple2D-19(音乐播放器)播放器的源码实现
使用 BASS 和 ImGui 实现音乐播放器 MusicPlayer. 将播放器和一个文件夹关联起来,程序刚开始运行的时候就从该文件夹加载所有音频文件.而文件夹的路径则保存在配置文件中,所以程序的第 ...
- WDA-FPM-4-用OVP做查询跳转到明细
转载:https://www.cnblogs.com/sapSB/p/10100697.html FPM四:用OVP做查询跳转到明细 前面做了查询的UIBB配置,在这边可以直接复用,查询的feed ...
- webserive学习记录6-页面请求webservice
前面都是通过JAVA代码访问webservice服务,下面将介绍通过javascript,jquery访问webservice服务并介绍过过servlet解决跨域问题的方法. 服务端 编写服务代码,解 ...
- 迷你MVVM框架 avalonjs 学习教程13、模板引用
稍为复杂一点的网站都是多个前端工程师合作而成,因此分工是必需的.简单一点的分工就是一个人负责一个频道,某个页面是由一个人全部做的:但如果涉及到一个页面非常复杂,需要多个人同时动工呢?于是到模板的出场时 ...
- hbase orm中间层hbasedao
博客园发布文章的体验太差,Markdown的支持巨烂无比,尝试了富文本编辑,太麻烦,遂作罢.想看的跳转到这两个连接吧 树莓派的奇幻漂流 github
- hive sql 随机抽样
create table daizk.IOS_matrix_sex asselect *from zhujx.1029_IOS_features_replce_nullwhere sex = 'M'u ...
- Week4-作业1:阅读笔记与思考
我在这三天时间里阅读了<构建之法>的第四章和第十七章,产生了一些疑问和深层次的思考. 第四章 问题1: 书中第68页提到“注释(包括所有源代码)应该只用ASCII字符,不要用中文或其他特殊 ...
- VirtualBox中的虚拟机在Ubuntu 下无法启动之问题解决
我通过重新安装box解决的.发现,box安装,直接apt install有可能会出现虚拟机无法启动的问题. 一.添加VirtualBox的源并安装5.1版本virtualbox官网:https://w ...
- 120. Triangle(Array; DP)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...