我正在junit为我的方法编写一个测试用例service,它有一个外部调用jpa repository,我想这样做mock。
此方法findall(Pageable pageable)返回page由映射器映射的实体Mapstruct's。但是,NPE当我assert返回时,我得到了一个。可能有一些我遗漏的东西,我不确定如何mock调用这个方法。
我试过像这样编写测试用例测试用例:
public class myTestclass {
@Test
public void testFindAllUser() {
User user1 = new User();
user1.setId(Long.valueOf(1));
User user2 = new User();
user2.setId(Long.valueOf(2));
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(asset2);
Pageable pageable = PageRequest.of(0, 5);
Page<User> userPage = new PageImpl<>(userList, pageable, userList.size());
Page<UserDto> userDtoPage = null;
Mockito.when(userRepositoryMock.findAll(pageable)).thenReturn(userPage);
Mockito.when(userPage.map(userMapperMock::toDto)).thenReturn(userDtoPage);// expecting to mock this object in
// some other way.
assertThat(userService.findAll(pageable)).isEqualTo(userDtoPage); // throws NPE
}
}
我正在为其编写测试用例的方法:
public Page<UserDto> findAll(Pageable pageable)
{
return userRepository.findAll(pageable).map(userMapper::toDto);
}
这是我的映射器类:
@Mapper(componentModel = "spring", uses = { FarmerMapper.class })
public interface UserMapper extends EntityMapper<UserDto, User> {
UserDto toDto(User user);
User toEntity(UserDto userDto);
}
mapper模拟该方法toDto以使其返回页面的正确方法是什么userDto?
喵喔喔
相关分类