title: How to Mock Pageable object using Mockito?
body: <p>I am developing <code>Spring Boot + Data JPA + Mockito</code> example. I've developed test case for the <code>Pagination</code> repository method, but its giving me below error.</p>
  
  <pre><code>org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
  Misplaced or misused argument matcher detected here:
  
  -&gt; at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
  -&gt; at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
  -&gt; at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
  -&gt; at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
  
  You cannot use argument matchers outside of verification or stubbing.
  Examples of correct usage of argument matchers:
      when(mock.get(anyInt())).thenReturn(null);
      doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
      verify(mock).someMethod(contains("foo"))
  
  This message may appear after an NullPointerException if the last matcher is returning an object 
  like any() but the stubbed method signature expect a primitive argument, in this case,
  use primitive alternatives.
      when(mock.get(any())); // bad use, will raise NPE
      when(mock.get(anyInt())); // correct usage use
  
  Also, this error might show up because you use argument matchers with methods that cannot be mocked.
  Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
  Mocking methods declared on non-public parent classes is not supported.
  
      at com.xxxx.EmployeeServiceTest.test_findAllEmployeesPaginated(EmployeeServiceTest.java:152)
      at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
      at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
      at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
      at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
      at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@GetMapping()
  public ResponseEntity&lt;Page&lt;Division&gt;&gt; getAllDivisionsPaginated(
      @RequestParam(required = false, value="page") Integer page, @RequestParam(required = false, value = "size") Integer size,
      @RequestParam(required = false) String sort,
      @RequestParam(required = false) String direction,
      Pageable pageable){
  
      Page&lt;Division&gt; divisions = divisionService.findAllDivisions(pageable, page, size, sort, direction);
      return new ResponseEntity&lt;&gt;(divisions, HttpStatus.OK);
  }
  </code></pre>
  
  <p><strong>Method:</strong></p>
  
  <pre><code>public Page&lt;Division&gt; findAllEmployees(Pageable pageableReq, Integer page, Integer size, String sortBy, String direction) {
      Pageable pageable = Utils.sort(pageableReq, page, size, sortBy, direction);
      return employeeRepository.findByStatus(StatusEnum.A, pageable);
  }
  </code></pre>
  
  <p><strong>Another method:</strong></p>
  
  <pre><code>public static Pageable sort(Pageable pageableReq, Integer page, Integer size, String sortBy, String direction) {
       Pageable pageable = null;
       if (page != null &amp;&amp; size != null) {
           if (direction.equalsIgnoreCase("ASC"))
               pageable = PageRequest.of(page, size, Sort.by(sortBy).ascending());
           else if (direction.equalsIgnoreCase("DESC"))
               pageable = PageRequest.of(page, size, Sort.by(sortBy).descending());
       } else {
           pageable = pageableReq;
       }
      return pageable;
  }
  </code></pre>
  
  <p><strong>Test class:</strong></p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({StatusEnum.class})
  public class EmployeeServiceTest {
  
      @Mock
      private Employee employeeMock;
  
      @InjectMocks
      private EmployeeServiceImpl employeeServiceImpl;
  
      @Mock
      private EmployeeRepository employeeRepositoryMock;
  
      @Mock
      private EmployeeDto employeeDtoMock;
  
      @Mock
      private StatusEnum statusEnum;
  
      @Mock
      private Exception ex;
  
      @Mock
      private Pageable pageableMock;
  
      @Mock
      private Utils utils;
  
      @Mock
      private Page&lt;Employee&gt; employeePage;
  
      @Before
      public void setup() {
          mockStatic(StatusEnum.class);       
      }
  
      @Test
      public void test_findAllEmployeesPaginated() {
          Pageable pageable = PageRequest.of(0, 8);
          when(Utils.sort(pageable, anyInt(), anyInt(), anyString(), anyString())).thenReturn(pageableMock);
          when(employeeRepositoryMock.findByStatus(StatusEnum.A, pageableMock)).thenReturn(employeePage);
      }   
  }
  </code></pre>