简体   繁体   English

如何使用Mockito模拟Mapstruct的toDto方法的page.map(mapper :: toDto)

[英]How to mock Mapstruct's toDto method using Mockito for page.map(mapper::toDto)

I am writing a junit test case for my service method which has an external call to jpa repository , which I would like to mock . 我正在为我的service方法编写一个junit测试用例,该用例具有对jpa repository的外部调用,我想对其进行mock
This method findall(Pageable pageable) returns a page of entities which is being mapped by Mapstruct's mapper. 此方法findall(Pageable pageable)返回由Mapstruct's映射器映射的实体page However, I get an NPE when I assert the return. 但是,当我assert退货时,我得到了NPE There might be something which I am missing, I am not sure how to mock this method call. 我可能缺少一些东西,我不确定如何mock此方法调用。

I tried writing test case like this Test case: 我试着像这样写测试用例

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
}
}

The method for which i am writing the test case: 我正在编写测试用例的方法:

public Page<UserDto> findAll(Pageable pageable) 
{

return userRepository.findAll(pageable).map(userMapper::toDto);
}

This is my mapper class: 这是我的映射器类:

@Mapper(componentModel = "spring", uses = { FarmerMapper.class })

public interface UserMapper extends EntityMapper<UserDto, User> {

UserDto toDto(User user);

User toEntity(UserDto userDto);
}

What is the right way to mock the mapper method toDto so that it returns a page of userDto ? 模拟mapper方法toDto以便返回userDto页面的userDto什么?

Since the .map uses UserMapper's toDto Method to convert each element of the page, I mocked multiple elements of userMapper to resolve this problem. 由于.map使用UserMapper's toDto方法转换页面的每个元素,因此我userMapper多个元素来解决此问题。

Hence I created 2 pages, one for User and another for UserDto . 因此,我创建了2页,一个用于User ,另一个用于UserDto So my test case was modified as 所以我的测试用例被修改为

@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);
}

Now, if there are more number of elements to be added to the Page, I would mock the same toDto for all of the elements. 现在,如果有更多元素要添加到页面,我将为所有元素模拟相同的toDto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM