TDD(Test Drive Development)测试驱动开发技术概念,从需求出发设计一个预期结果的测试用例,通过不断地编码和重构,使得用例通过测试,从而保证代码的质量和可控性。
示例:对Controller进行测试,通过返回结果评价Controller的设计
1. jsp页
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>
2. 普通Bean
DemoService:
package com.service;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
public String saySomething() {
return "hello";
}
}
3. Controller和RestController
DemoNormalController:
package com.controller;
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.service.DemoService;
@Controller
public class DemoNormalController {
@Autowired
DemoService demoService;
@RequestMapping(value="/testNormal")
public String testNormal(Model model) {
model.addAttribute("msg", demoService.saySomething());
return "page";
}
}
DemoMyRestController:
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.service.DemoService;
@RestController
public class DemoMyRestController {
@Autowired
DemoService demoService;
@RequestMapping(value="/testRest", produces="text/plain; charset=UTF-8")
public String testRest() {
return demoService.saySomething();
}
}
4. Test类
TestControllerIntegrationTests:
package com.mock;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.config.MvcConfig;
import com.service.DemoService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MvcConfig.class})
// This annotation indicates that ApplicationContext which
// is loaded by this annotated class should be WebApplicationContext.
// This annotation has a default resource path(i.e. "src/main/webapps") for test to web application root in maven,
// but in spring MVC, this path should be overridden by "src/main/resources".
@WebAppConfiguration("src/main/resources")
public class TestControllerIntegrationTests {
private MockMvc mockMvc; // main entry point for server-side Spring MVC test support
@Autowired
private DemoService demoService;
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session; // unused
@Autowired
MockHttpServletRequest request; // unused
@Before
public void init() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testNormalController() throws Exception {
this.mockMvc.perform(get("/testNormal")) //模拟测试请求
.andExpect(status().isOk()) // 预期返回状态为200
.andExpect(view().name("page")) // 预期view名为page
.andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp")) //预期request的url
.andExpect(model().attribute("msg", demoService.saySomething())); //预期model的msg属性为“hello”
}
@Test
public void testRestController() throws Exception {
this.mockMvc.perform(get("/testRest"))
.andExpect(status().isOk())
.andExpect(content().contentType("text/plain; charset=UTF-8"))
.andExpect(content().string(demoService.saySomething()));
}
}