【发布时间】:2017-12-05 16:54:00
【问题描述】:
我在为使用 Thymeleaf 的 Spring boot MVC Web 项目测试具有分页功能的控制器时遇到问题。我的控制器如下:
@RequestMapping(value = "admin/addList", method = RequestMethod.GET)
public String druglist(Model model, Pageable pageable) {
model.addAttribute("content", new ContentSearchForm());
Page<Content> results = contentRepository.findContentByContentTypeOrByHeaderOrderByInsertDateDesc(
ContentType.Advertisement.name(), null, pageable);
PageWrapper<Content> page = new PageWrapper<Content>(results, "/admin/addList");
model.addAttribute("contents", results);
model.addAttribute("page", page);
return "contents/addcontents";
}
我已尝试使用以下测试段来计算内容项(最初它将返回 0 分页项)。
andExpect(view().name("contents/addcontents"))
.andExpect(model().attributeExists("contents"))
.andExpect(model().attribute("contents", hasSize(0)));
但出现以下错误(测试很好,在分页之前):
java.lang.AssertionError: Model attribute 'contents'
Expected: a collection with size <0>
but: was <Page 0 of 0 containing UNKNOWN instances>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
我有护目镜,但在这里没有运气。谁能帮我举个例子来测试一个处理存储库中的可分页对象的控制器吗?
有没有其他方法可以用分页测试列表?请帮忙。
提前致谢!
【问题讨论】:
标签: spring-mvc junit mockito spring-mvc-test