title: How to test Spring MVC controller with pagination?
body: <p>I am facing a problem to test a controller with pagination for my <strong>Spring boot MVC</strong> web project which uses <strong>Thymeleaf</strong> . My controller is as follows:</p>
  
  <pre><code>@RequestMapping(value = "admin/addList", method = RequestMethod.GET)
      public String druglist(Model model, Pageable pageable) {
  
          model.addAttribute("content", new ContentSearchForm());
          Page&lt;Content&gt; results = contentRepository.findContentByContentTypeOrByHeaderOrderByInsertDateDesc(
                  ContentType.Advertisement.name(), null, pageable);
  
  
          PageWrapper&lt;Content&gt; page = new PageWrapper&lt;Content&gt;(results, "/admin/addList");
          model.addAttribute("contents", results);
          model.addAttribute("page", page);
          return "contents/addcontents";
  
      }
  </code></pre>
  
  <p>I have tried with  this following test segment which will count the contents items (initially it will return 0 item with pagination).</p>
  
  <pre><code>andExpect(view().name("contents/addcontents"))
      .andExpect(model().attributeExists("contents"))
      .andExpect(model().attribute("contents", hasSize(0)));
  </code></pre>
  
  <p>but getting this following error ( test was fine, before pagination): </p>
  
  <pre><code> java.lang.AssertionError: Model attribute 'contents'
  Expected: a collection with size &lt;0&gt;
       but: was &lt;Page 0 of 0 containing UNKNOWN instances&gt;
   at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
  </code></pre>
  
  <p>I have goggled but no luck here. Can anybody help me with an example to test a controller which deals with <strong>pageable object from repository</strong>?</p>
  
  <p>Is there any alternate way exist to test list with pagination? Please help.</p>
  
  <p>Thanks in advance!</p>