1 回答
TA贡献1735条经验 获得超5个赞
由于.map使用UserMapper's toDto方法来转换页面的每个元素,我模拟了多个元素userMapper来解决这个问题。
因此,我创建了 2 个页面,一个User用于UserDto. 所以我的测试用例被修改为
@Test
public void testFindAllUser() {
User user1 = new User();
user1.setId(DEFAULT_ID);
User user2 = new User();
user2.setId(2L);
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
Pageable pageable = PageRequest.of(0, 5);
Page<User> userPage = new PageImpl<>(userList, pageable, userList.size());
UserDto userDto1 = new UserDto();
userDto1.setId(Long.valueOf(1));
UserDto userDto2 = new UserDto();
userDto2.setId(Long.valueOf(2));
List<UserDto> userDtoList = new ArrayList<>();
userDtoList.add(userDto1);
userDtoList.add(userDto2);
Page<UserDto> userDtoPage = new PageImpl<>(userDtoList, pageable, userDtoList.size());
Mockito.when(userMapperMock.toDto(user1)).thenReturn(userDto1);
Mockito.when(userMapperMock.toDto(user2)).thenReturn(userDto2); //Mocking the toDto method.
Mockito.when(userRepositoryMock.findAll(pageable)).thenReturn(userPage);
assertThat(userService.findAll(pageable)).isEqualTo(userDtoPage);
}
现在,如果有更多的元素要添加到页面,我会对toDto所有元素进行相同的模拟。
添加回答
举报