测试是保证软件质量的关键,在之前的讲解中只是介绍了简单的测试,下面要进行一些和
- Spring
相关的测试,主要涉及控制器的测试。
- MVC
为了测试
项目通常不需要启动项目,我们需要一些
相关的模拟对象,比如:
- Servlet
,
- MockMVC
,
- MockHttpServletRequest
,
- MockHttpServletResponse
等。
- MockHttpSession
在
里,我们使用
- Spring
指定加载的
- @WebAppConfiguration
是一个
- ApplicationContext
。
- WebAppConfiguration
在下面的示例里面借助
和
- JUnit
,分别演示对普通页面转向形控制器和
- Spring TestContext framework
进行测试。
- RestController
- <!-- spring-test -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring-framework.version}</version>
- <scope>test</scope>
- </dependency>
- <!-- junit -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
这里
说明这些包的存活是在
- <scope>test</scope>
周期,也就是意味着发布时我们将不包含这些
- test
包。
- jar
在
下新增
- src/main/java
类,代码如下所示:
- DemoService
- package org.light4j.springMvc4.service;
- import org.springframework.stereotype.Service;
- @Service
- public class DemoService {
- public String saySomething(){
- return "hello";
- }
- }
在
下新建
- src/test/java
类,代码如下:
- TestControllerIntegrationTests
- package org.light4j.springMvc4.web;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.light4j.springMvc4.MyMvcConfig;
- import org.light4j.springMvc4.service.DemoService;
- 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;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(classes = {MyMvcConfig.class})
- @WebAppConfiguration("src/main/resources") //①
- public class TestControllerIntegrationTests {
- private MockMvc mockMvc; //②
- @Autowired
- private DemoService demoService;//③
- @Autowired
- WebApplicationContext wac; //④
- @Autowired
- MockHttpSession session; //⑤
- @Autowired
- MockHttpServletRequest request; //⑥
- @Before //7
- public void setup() {
- mockMvc =
- MockMvcBuilders.webAppContextSetup(this.wac).build(); //②
- }
- @Test
- public void testNormalController() throws Exception{
- mockMvc.perform(get("/normal")) //⑧
- .andExpect(status().isOk())//⑨
- .andExpect(view().name("page"))//⑩
- .andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp"))//11
- .andExpect(model().attribute("msg", demoService.saySomething()));//12
- }
- @Test
- public void testRestController() throws Exception{
- mockMvc.perform(get("/testRest")) //13
- .andExpect(status().isOk())
- .andExpect(content().contentType("text/plain;charset=UTF-8"))//14
- .andExpect(content().string(demoService.saySomething()));//15
- }
- }
①
注解在类上,用来声明加载的
- @WebAppConfiguration
是一个
- ApplicationContext
。它的属性指定的是
- WebApplicationContext
资源的位置,默认为
- Web
,本例修改为
- src/main/webapp
。 ②
- src/main/resource
模拟
- MockMvc
对象,通过
- MVC
进行初始化。 ③ 可以在测试用例中注入
- MockMvcBuilders.webAppContextSetup(this.wac).build()
的
- Spring
。 ④ 可注入
- Bean
。 ⑤ 可注入模拟的
- WebApplicationContext
,此处仅作演示,没有使用。 ⑥ 可注入模拟的
- http session
,此处仅作演示,没有使用。 ⑦
- http request
在测试开始前进行的初始化工作。 ⑧ 模拟向
- @Before
进行
- /normal
请求。 ⑨ 预期控制返回状态为
- get
. ⑩ 预期
- 200
的名称为
- view
。 11 预期页面转向的真正路径为
- page
。 12 预期
- /WEB-INF/classes/views/page.jsp
里面的值是
- model
返回值
- demoService.saySomething()
。 13.模拟向
- hello
进行
- /testRest
请求。
- get
14 预期返回值的媒体类型是。 15 预期返回值的内容为
- text/plain;charset=UTF-8
返回值
- demoService.saySomething()
。
- hello
此时,运行该测试,效果如下图所示:
在
下新增
- src/main/java
类,代码如下所示:
- NormalController
- package org.light4j.springMvc4.web;
- import org.light4j.springMvc4.service.DemoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class NormalController {
- @Autowired
- DemoService demoService;
- @RequestMapping("/normal")
- public String testPage(Model model){
- model.addAttribute("msg", demoService.saySomething());
- return "page";
- }
- }
在
下新建
- src/main/resources/view
- 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>
在
下新增
- src/main/java
类,代码如下所示:
- RestController
- package org.light4j.springMvc4.web;
- import org.light4j.springMvc4.service.DemoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @RestController
- public class MyRestController {
- @Autowired
- DemoService demoService;
- @RequestMapping(value = "/testRest" ,produces="text/plain;charset=UTF-8")
- public @ResponseBody String testRest(){
- return demoService.saySomething();
- }
- }
效果如下图所示:
打赏 欢迎关注人生设计师的微信公众账号地址:点击查看
- github
码云地址:点击查看
来源: https://juejin.im/entry/5a12112d518825293b4fe8c9