我正在尝试编写一个简单的API来检索分页数据。我的程序在未分页时运行并“查找全部”。但是,当我从模拟存储库(Mockito)调用findAll(Pageable)时,它将返回null。
TaskService.java
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<TaskResponse> findAll(Pageable pageRequest);
}
TaskServiceImpl.java
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<TaskResponse> findAll(Pageable pageRequest) {
Page<TaskEntity> tasks = this.taskRepository.findAll(pageRequest); <=== HERE IT RETURNS NULL
return tasks;
}
TaskRepository.java
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<TaskEntity, Long>, PagingAndSortingRepository<TaskEntity, Long> {
Page<TaskEntity> findAll(Pageable pageRequest);
}
TaskServiceTest.java
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<TaskResponse> tasks = taskService.findAll(pageRequest).getContent();
assertThat(tasks.size(), equalTo(4));
verify(taskRepository).findAll();
}
}
在TaskServiceImpl.java中,.findAll(pageRequest)返回null。我对Mockito不太熟悉,想知道使用它创建模拟存储库是否会引起问题?当我不分页地执行.findAll()时,它的效果很好。我如何使用PagingAndSortingRepository中的findAll(Pageable)方法有问题。谢谢!
答案 0 :(得分:2)
您模拟不带参数的对findAll
的调用,只有这样才能返回您设置的内容。要模拟对需要参数的重载的调用,您需要调用when(taskRepository.findAll(org.mockito.Matchers.isA(Pageable.class)).thenReturn(...)
。您可能应该静态导入org.mockito.Mathers
来提高可读性。如果要验证有关传递对象的信息,也可以使用ArgumentCaptor。
答案 1 :(得分:1)
您可以通过两种方法解决此问题。
使用模拟:
Page<TaskEntity> tasks = Mockito.mock(Page.class);
Mockito.when(this.taskRepository.findAll(org.mockito.Matchers.isA(Pageable.class))).thenReturn(tasks);
或者,使用类实例化:
List<TaskEntity> tasks = new ArrayList<>();
Page<TaskEntity> pagedTasks = new PageImpl(tasks);
Mockito.when(this.taskRepository.findAll(pagedTasks)).thenReturn(pagedTasks);