title: Spring Boot pagination - Mockito repository findAll(Pageable) returns null
body: <p>I am trying to write a simple API to retrieve paged data. My program runs and 'finds all' when not paged. However when I call findAll(Pageable) from the mock repository (Mockito), it returns null.</p>
  
  <p>TaskService.java</p>
  
  <pre class="lang-java prettyprint-override"><code>import com.example.task.dto.TaskResponse;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.Pageable;
  
  import java.util.List;
  
  public interface TaskService {
      Page&lt;TaskResponse&gt; findAll(Pageable pageRequest);
  }
  </code></pre>
  
  <p>TaskServiceImpl.java</p>
  
  <pre class="lang-java prettyprint-override"><code>import com.example.task.domain.TaskEntity;
  import com.example.task.dto.TaskResponse;
  import com.example.task.repository.TaskRepository;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageImpl;
  import org.springframework.data.domain.Pageable;
  import org.springframework.stereotype.Service;
  
  import java.lang.reflect.Array;
  import java.util.ArrayList;
  import java.util.List;
  
  public class TaskServiceImpl implements TaskService {
      private TasksRepository tasksRepository;
  
      public TaskServiceImpl(TasksRepository tasksRepository) {
          this.tasksRepository = tasksRepository;
      }
  
      @Override
      public Page&lt;TaskResponse&gt; findAll(Pageable pageRequest) {
          Page&lt;TaskEntity&gt; tasks = this.taskRepository.findAll(pageRequest); &lt;=== HERE IT RETURNS NULL
  
          return tasks;
      }
  </code></pre>
  
  <p>TaskRepository.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.task.repository;
  
  import com.example.task.domain.TaskEntity;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.Pageable;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.data.repository.PagingAndSortingRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface TaskRepository extends JpaRepository&lt;TaskEntity, Long&gt;, PagingAndSortingRepository&lt;TaskEntity, Long&gt; {
      Page&lt;TaskEntity&gt; findAll(Pageable pageRequest);
  }
  
  </code></pre>
  
  <p>TaskServiceTest.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.task.service;
  
  import com.example.task.domain.TaskEntity;
  import com.example.task.repository.TaskRepository;
  import org.junit.jupiter.api.Test;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageRequest;
  import org.springframework.data.domain.Pageable;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.Matchers.equalTo;
  import static org.mockito.Mockito.*;
  
  class TaskServiceTest {
      private TaskRepository taskRepository = mock(TaskRepository.class);
      private TaskServiceImpl taskService = new TaskServiceImpl(taskRepository);
  
      @Test
      void FindAll_ReturnsAPagedListOfTasks() {
          when(taskRepository.findAll()).thenReturn(Arrays.asList(
                  new TaskEntity(1L, "Fake task 1"),
                  new TaskEntity(2L, "Fake task 2"),
                  new TaskEntity(3L, "Fake task 3"),
                  new TaskEntity(4L, "Fake task 4")
          ));
  
          Pageable pageRequest = PageRequest.of(0, 4);
          List&lt;TaskResponse&gt; tasks = taskService.findAll(pageRequest).getContent();
  
          assertThat(tasks.size(), equalTo(4));
  
          verify(taskRepository).findAll();
      }
  }
  </code></pre>
  
  <p>In TaskServiceImpl.java .findAll(pageRequest) returns null. I am not very familiar with Mockito and wondering if creating a mock repository with it is causing issues? It works just fine when I do .findAll() with no pagination. Is there something wrong with how I am using findAll(Pageable) method from the PagingAndSortingRepository. Thanks!</p>