2
我一直在使用的Mockito來測試我的春節控制器的方法,但很困惑與下列失敗測試的行爲:測試在春天空模型屬性
控制器:
@RequestMapping(value = "/getPage", method = RequestMethod.GET)
public String getPage(Model model) {
String myString = myService.someMethod();
model.addAttribute("myString", myString);
return "myTemplate";
}
測試:
@Test public void testGetPage() throws Exception {
String myString = null;
when(mockService.someMethod()).thenReturn(myString);
mockMvc.perform(get("/getPage"))
.andExpect(status().isOk())
.andExpect(model().size(1)) // this passes
.andExpect(model().attribute("myString", myString)) // fails: "Model attribute 'myString' does not exist
.andExpect(model().attributeExists("myString")) // fails: "Model attribute 'myString' does not exist
;
}
那麼什麼是測試檢測何時返回model()。size(1)如果不是myString變量?有沒有辦法可以列出與模型相關的所有屬性?