Skip to content
Advertisement

How to mock MapStruct nested mapper in JUnit5

I have been searching a solution for about a day now. I still cannot produce a working example.

My problem is simple. I have a mapper uses another mapper:

@Mapper(componentModel = "spring", uses = {RoleMapper.class})
public interface UserMapper {

    /**
     * Converts User DTO to User JPA Entity
     * @Mapping annotation is used to convert fields with different names
     * @param dto
     * @return
     */
    @Mappings({
            @Mapping(target = "identityNo", source = "dto.userIdentity"),
            @Mapping(target = "role", source = "dto.roleDTO")
    })
    User dtoToEntity(UserDTO dto);

    /**
     * Converts User JPA Entity to User DTO
     * @Mapping annotation is used to convert fields with different names
     * @param entity
     * @return
     */
    @Mappings({
            @Mapping(target = "userIdentity", source = "entity.identityNo"),
            @Mapping(target = "roleDTO", source = "entity.role")
    })
    UserDTO entityToDto(User entity);

}

@Mapper(componentModel = "spring")
public interface RoleMapper {

    Role roleDtoToEntity(RoleDTO dto);
    RoleDTO roleEntityToDto(Role entity);
}

My test class which tests mapper works as it should:

class UserMapperTest {

    private UserMapper mapper = Mappers.getMapper(UserMapper.class);

    @Test
    void dtoToEntity() {

        User user = new User();
        user.setName("john");
        user.setSurname("doe");
        user.setIdentityNo("11111111111");
        user.setRole(new Role("ROLE_ADMIN"));

        UserDTO dto = mapper.entityToDto(user);

        Assertions.assertEquals(user.getName(), dto.getName());
        Assertions.assertEquals(user.getSurname(), dto.getSurname());
        Assertions.assertEquals(user.getIdentityNo(), dto.getUserIdentity());
        Assertions.assertEquals(user.getRole().getName(), dto.getRoleDTO().getName());
    }
}

However, NullPointerException is thrown on the line where roleMapper is called in automatically generated impl class UserMapperImpl:

img

it comes to my basic problem, how to mock or autowire a nested mapper class?

Advertisement

Answer

Nested mapper is null, because Spring context is not loaded. Due to this, @Autowired is not working.

Solution is to inject nested mappers via ReflectionTestUtils.

@InjectMocks
private UserMapper mapper = UserMapperImpl.INSTANCE;

@Before
public void init() {
    RoleMapper roleMapper = Mappers.getMapper(RoleMapper.class);
    ReflectionTestUtils.setField(mapper, "roleMapper", roleMapper);
}
Advertisement