2015-07-21 207 views
0

這是控制器我有:爲什麼這個MockMvc測試失敗?

@Controller 
@EnableWebMvc 
@RequestMapping(value = "/spittles") 
public class SpittleController { 

    SpittleRepository spittleRepository; 

    @RequestMapping(method = RequestMethod.GET) 
    public String getSpittles(Model model) { 
     model.addAttribute(spittleRepository.findSpittles(10, 10)); 
     return "spittles"; 
    } 

    @Autowired 
    public void setSpittleRepository(SpittleRepository spittleRepository) { 
     this.spittleRepository = spittleRepository; 
    } 

} 

和查看我(spittles)

<c:forEach items="${spittleList}" var="spittle"> 
    ${spittle.message} 
</c:forEach> 

工作正常..

但是這個測試失敗:

SpittleController spittleController = new SpittleController(); 
    spittleController.setSpittleRepository(mockRepository); 

    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(spittleController) 
      .setSingleView(new InternalResourceView("/WEB-INF/views/spittles.jsp")).build(); 

    mockMvc.perform(get("/spittles")) 
      .andExpect(view().name("spittles")) 
      .andExpect(model().attributeExists("spittleList")); 

與m essage:

java.lang.AssertionError: Model attribute 'spittleList' does not exist 

但在JSP中,它看起來像模型具有屬性spittleList,我缺少什麼?

回答

0

指定名稱後問題仍然存在?

model.addAttribute("spittleList", spittleRepository.findSpittles(10, 10)); 
+0

沒有它。不。 –

+0

那麼它解決了你的問題? – Saraph

0

你好你可以指望嘲笑你的服務的幫助,可以查看如下:

預期(spittleRepository.findSpittles(10,10))andReturn(預期輸出)