Spring框架如何測試
過去寫單獨文件的時候我們經常使用的是JUNIT標注來實現(xiàn)一個方法的測試,但是當我們使用WEBMVC進行程序代碼編寫的時候我們已經無法使用了過去那種測試了,這個需求Spring已經幫我們想到了,提供了優(yōu)秀的庫Spring-test幫助我們。
必備知識點
@RunWith
用于指定
junit運行環(huán)境,是junit提供給其他框架測試環(huán)境接口擴展,為了便于使用spring的依賴注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作為Junit測試環(huán)境,例如:@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
導入配置文件,例如:
@ContextConfiguration(classes = { MyMvcConfig.class })
@Transactional
@WebAppConfiguration
WebAppConfiguration注解在類上, 用來聲明加載的ApplicationContex是一個WebApplicationContext。 它的屬性指定的是Web資源的位置,默認為src/main/webapp
@Before
在測試開始前進行的初始化工作
MockMvc
MockMvc模擬MVC對象, 通過MockMvcBuilders.webAppContextSetup(this.wac).build()初始化。
MockHttpSession
可注入模擬的
http session
MockHttpServletRequest
可注入模擬的
http request
mockMvc.perform(get("/normal"))
模擬向/normal進行get請求。
.andExpect(status().isOk())
返回狀態(tài)碼
.andExpect(view().name("demo"))
設置返回view的名稱為demo
.andExpect(forwardedUrl("/WEB-INF/classes/views/demo.jsp"))
指定view存放的路徑
.andExpect(model().attribute("msg",demoService.demo()))
預期 model 里 的 值 是
demoService. saySomething()返回 值 hello。
.andExpect(content().contentType("text/plain;charset=UTF-8"))
返回 值 的 媒體 類型 為
text/plain;charset=UTF-8
.andExpect(content().string(demoService.saySomething()))
預期 返回 值 的 內容 為
demoService.demo()
實際操作
正常的控制器
package com.haojishu.demo.web;
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.haojishu.demo.service.DemoService;
@Controller
public class HelloController {
@Autowired
DemoService demoService;
@RequestMapping("/index")
public String hello(Model model) {
model.addAttribute("msg", demoService.demo());
return "index";
}
}
測試控制器
package com.haojishu.demo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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.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.haojishu.demo.service.DemoService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyMvcConfig.class })
@WebAppConfiguration("src/main/resource")
public class TestController {
private MockMvc mockMvc;
@Autowired
private DemoService demoService;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testIndexController() throws Exception {
mockMvc.perform(get("/index")).andExpect(status().isOk()).andExpect(view().name("index"))
.andExpect(forwardedUrl("/WEB-INF/classes/views/index.jsp"))
.andExpect(model().attribute("msg", demoService.demo()));
}
}