Search code examples
javajunitjava-8mockitomapstruct

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.
This method findall(Pageable pageable) returns a page of entities which is being mapped by Mapstruct's mapper. However, I get an NPE when I assert the return. There might be something which I am missing, I am not sure how to mock this method call.

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?


Solution

  • 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.

    Hence I created 2 pages, one for User and another for 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.