为什么这个MockMvc测试失败了?

时间:2015-07-21 09:21:02

标签: java spring 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"));

带有消息:

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

但是在jsp中,看起来该模型具有属性 spittleList ,我缺少什么?

2 个答案:

答案 0 :(得分:0)

指定名称后,问题是否仍然存在?

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

答案 1 :(得分:0)

您好您可以帮助我们嘲笑您的服务,如下所示:

期望(spittleRepository.findSpittles(10,10))。和返回(预期输出)