spring3.2終於出ga版了, 發現spring test的改進最大,添加了對 springmvc的測試 java
看了看文檔,將使用方法整理以下: web
1,能夠使用 @WebAppConfiguration來標明是web應用測試, @ContextConfiguration來指定配置文件,其餘的和測試相同 spring
2,主要用到三個類: 1,MockMvc及MockMvcBuilders, 用來生成當前的測試環境,後者是生成MockMvc的 json
2,MockMvcRequestBuilders , 模擬http請求 cookie
3,MockMvcResultMatchers ,對返回結果進行斷言 mvc
3,這幾個類都提供了鏈式操做,寫代碼的時候就很舒服了,代碼也好看多了,下面就詳細說說這幾個類 app
3.1 MockMvcBuilders 異步
這個類只有兩個方法:DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) 和 StandaloneMockMvcBuilder standaloneSetup(Object... controllers) ,區別是 前者依賴Spring上下文,所以這個要加載配置文件 async
StandaloneMockMvcBuilder 繼承自 DefaultMockMvcBuilder post
畢竟spring的配置文件中沒有web.xml中那些filter的信息,沒法完整模擬web環境, 所以 DefautlMockBuilder中有 addFilter(Filter filter) 和 addFilters(Filter ...)兩個方法,還有一個RequestBuilder,能夠定製默認的request, 還有一些 alwaysExpect() ,alwaysDo等方法,用於添加默認斷言
配置好Builder後,而後 調用build()方法 生成 MockMvc對象, 咱們就能夠用來測試了
3.2 MockMvcRequestBuilders
這個類裏面全是靜態方法, 提供http操做方法如: get,post,delete,put,fileupload , 還有一個request方法,能夠本身指定 前面那類型, 還有一個 asyncDispatch(MvcResult mvcResult),應該是異步執行,這個方法沒用過,有時間看看是幹嗎用的
這些方法返回的都是 RequestBuilder的子類, 針對不一樣http請求類型作了一些拓展,都是鏈式操做很方便的
3.2 MockMvcResultMatchers , 這個是對返回結果進行斷言,也都是靜態方法,鏈式操做, 這些方法返回的都是 ResultMather的子類
提供的方法有:request(),handler(), model(),view(),flash(),forwardUrl(String expectUrl),redirectUrl(String expectUrl) ,status(),header(),content(),jsonPath() ,xpath(),cookie();
jsonPath 和 xpath 是針對json和xml格式數據的,具體格式請參考 http://goessner.net/articles/JsonPath/ 這篇文章。
jsonPath 操做依賴 jsonPath類, xpath沒用過應該也有相關依賴吧
下面放一個測試例子吧
public class SpringControllerTest { @Test public void json() throws Exception { MockMvc mockMvc = standaloneSetup(new PersonController()).build(); mockMvc.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.name").value("Lee")).andExpect(jsonPath("$.title").value("你好")); mockMvc.perform(post("/p2/haha").param("title", "你好吧")).andExpect(status().is(302)) .andExpect(view().name("redirect:/hello")).andExpect(model().attribute("title", "你好吧")); mockMvc.perform(get("/p2/haha2").param("title", "你好吧").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$.data[0].fullName").value("寶馬-進口-x6")); } @Controller private class PersonController { @RequestMapping(value = "/person/{name}") public Object get(@PathVariable String name) { Map person = new HashMap(); person.put("name", name); person.put("title", "你好"); return ViewUtils.createJsonView(person); } @RequestMapping(value = "/p2/haha") public Object post(String name, String title) { Map person = new HashMap(); person.put("name", name); person.put("title", title); return ViewUtils.createView("redirect:/hello", person); } @RequestMapping(value = "/p2/haha2",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8") @ResponseBody public Object getjson(String name, String title) { List<Car> cars = Lists.newArrayList(); cars.add(EntityFactory.createCar("寶馬-進口-x6", "x6", null, "進口")); JsonData data = new JsonData(); data.setData(BeanMapper.mapList(cars, CarDTO.class)); return data; } } }