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,我缺少什麼?
沒有它。不。 –
那麼它解決了你的問題? – Saraph