I am facing a problem to test a controller with pagination for my Spring boot MVC web project which uses Thymeleaf . My controller is as follows:
@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";
}
I have tried with this following test segment which will count the contents items (initially it will return 0 item with pagination).
andExpect(view().name("contents/addcontents"))
.andExpect(model().attributeExists("contents"))
.andExpect(model().attribute("contents", hasSize(0)));
but getting this following error ( test was fine, before pagination):
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)
I have goggled but no luck here. Can anybody help me with an example to test a controller which deals with pageable object from repository?
Is there any alternate way exist to test list with pagination? Please help.
Thanks in advance!