Spring Boot实战(四)Spring MVC基础 4.6 Spring MVC的测试
为了测试Web项目通常不需要启动项目,我们需要一些Servlet相关的模拟对象,比如:MockMVC、MockHttpServletRequest、MockHttpServletResponse、MockHttpSession等。
在Spring里,我们使用@WebAppConfiguration指定加载的ApplicationContext是一个WebApplicationContext是一个WebApplicationContext。
可能许多人,包括我自己以前也觉得测试有什么用,自己启动一下,点点弄弄,就像我们前面的例子不也都是这样测试的吗?其实在现实开发中,我们是先有需求的,也就是说先知道我们想要的是什么样的,然后按照我们想要的样子去开发。在这里我也要引入一个概念叫测试驱动开发(Test Driven Development,TDD),我们(设计人员)按照需求先写一个自己预期结果的测试用例,这个测试用例刚开始肯定是失败的测试,随着不断的编码和重构,最终让测试用例通过测试,这样才能保证软件的质量和可控性。
在下面的示例里我们借助JUnit和Spring TestContext framework,分别演示对普通页面转向形控制器和RestController进行测试。
示例
(1)测试依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
这里的<scope>test</scope> 说明这些包的存活是在test周期,也就意味着发布时我们将不包含这些jar包。
(2)演示服务:
package com.wisely.highlight_springmvc4.service;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
public String saySomething(){
return "hellow";
}
}
(3)测试用例,在src/test/java下:
package com.wisely.highlight_springmvc4.web.ch4_6;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import javax.ws.rs.POST;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
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 com.wisely.highlight_springmvc4.MyMvcConfig;
import com.wisely.highlight_springmvc4.service.DemoService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyMvcConfig.class})
@WebAppConfiguration("src/main/resources") // @WebAppConfiguration注解在类上,用来声明加载的ApplicationContext是一个WebApplicationContext。它的属性指定的是Web资源的位置,默认为 src/main/webapp,本例修改为 src/main/resources。
public class TestControllerIntegrationTests {
private MockMvc mockMvc; // MockMvc 模拟MVC对象,通过MockMvcBuilders.webAppContextSetup(this.wac).build()初始化。
@Autowired
private DemoService demoService; //可以在测试用例中注入Spring的Bean。
@Autowired
WebApplicationContext wac ; //可注入WebApplicationContext。
@Autowired
MockHttpSession session; //可注入模拟的http session,此处仅作演示,没有使用。
@Autowired
MockHttpServletRequest request; //可注入模拟的http request,此处仅作演示,没有使用。
@Before //@Before 在测试开始前进行的初始化工作。
public void setup(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testNormalController() throws Exception{
mockMvc.perform(get("/normal")) //模拟向/normal进行get请求,此处get的包eclipse不会自动提示,要手动写import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
.andExpect(status().isOk()) //预期控制返回状态为200。
.andExpect(view().name("page")) //预期view的名称为page。
.andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp")) //预期页面转向的真正路径为/WEB-INF/classes/views/page.jsp。
.andExpect(model().attribute("msg", demoService.saySomething())); //预期model里的值是demoService.saySomething()返回值hello。
}
public void testRestController() throws Exception{
mockMvc.perform(get("/testRest")) //模拟向/testRest进行get请求。
.andExpect(status().isOk())
.andExpect(content().contentType("text/plain;charset=UTF-8")) //预期返回值的媒体类型为text/plain;charset=UTF-8。
.andExpect(content().string(demoService.saySomething())); //预期返回值的内容为demoService.saySomething()返回值hello。
}
}
此时运行该测试效果如图
(4)编写普通控制器。
package com.wisely.highlight_springmvc4.web.ch4_6;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wisely.highlight_springmvc4.service.DemoService;
@Controller
public class NormalController {
@Autowired
DemoService demoService;
@RequestMapping("/normal")
public String testPage(Model model){
model.addAttribute("msg",demoService.saySomething());
return "page";
}
}
(5)编写普通控制器的演示页面,在 src/main/resources/views 下新建page.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test page</title>
</head>
<body>
<pre>
Welcome to Spring MVC world
</pre>
</body>
</html>
(6)编写RestController控制器:
package com.wisely.highlight_springmvc4.web.ch4_6;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.wisely.highlight_springmvc4.service.DemoService;
@RestController
public class MyRestController {
@Autowired
DemoService demoService;
@RequestMapping(value = "/testRest", produces="text/plain;charset=UTF-8")
public @ResponseBody String testRest(){
return demoService.saySomething();
}
}
(7)运行测试,效果如图: