JUnit-如何模拟MapStruct嵌套映射器 [英] JUnit - How to mock MapStruct nested mapper
问题描述
当我为服务层类运行单元测试时,我收到了一个烦人的NPE. 此类使用MapStruct中自动生成的映射器,该映射器内部使用另一个映射器(请参见Mapper批注中的"uses"属性):
I'm receiving an annoying NPE when I run a Unit Test for a service layer class. This class use an autogenerated mapper from MapStruct, which inside use another mapper (see in the Mapper annotation the "uses" attribute):
Mapper(componentModel = "spring", uses = {UserMapper.class})
public interface CandidateMapper extends EntityMapper<CandidateDTO, Candidate> {
@Mapping(target = "createdBy", ignore = true)
@Mapping(target = "createdDate", ignore = true)
@Mapping(target = "lastModifiedBy", ignore = true)
@Mapping(target = "lastModifiedDate", ignore = true)
@Mapping(target = "applications", ignore = true)
Candidate toEntity(CandidateDTO candidateDTO);
default Candidate fromId(Long id) {
if (id == null) {
return null;
}
Candidate candidate = new Candidate();
candidate.setId(id);
return candidate;
}
}
我的UnitTest是:
My UnitTest is:
@RunWith(SpringRunner.class)
public class CandidateServiceTest {
private CandidateService candidateService;
@MockBean
private UserRepository userRepository;
@MockBean
CandidateRepository candidateRepository;
@MockBean
UserDetailsService userDetailsService;
CandidateMapper candidateMapper = Mappers.getMapper(CandidateMapper.class);
UserMapper userMapper = Mappers.getMapper(UserMapper.class);
@Before
public void init() {
this.candidateService = new CandidateService(candidateRepository,
candidateMapper, userDetailsService, userMapper);
}
@Test
@WithMockUser(authorities = RolesConstants.ADMIN)
public void givenUser_whenGetCandidateOrCreateForLogin_create() {
// Pre-conditions
...
// Mocking data
...
// Given
given(userRepository.findOneByLogin(eq(login)))
.willReturn(Optional.of(user));
given(candidateRepository.findOneByUserLogin(eq(login)))
.willReturn(Option.of(candidate));
// When
CandidateDTO candidateDTO = candidateService.getCandidateOrCreateForLogin(login);
// Then
...
}
NPE通过此行引发:
The NPE is raised by this line:
candidateDTO.setUser( userMapper.toDto( candidate.getUser() ) );
在CandidateMapperImpl中,是因为候选人MapperImpl中的userMapperImpl实例(变量名称userMapper)为空.
in the CandidateMapperImpl because the userMapperImpl instance (variable name userMapper) inside the candidateMapperImpl is null.
当我使用spring-boot:run启动应用程序时,仅使用UnitTest时,不会发生这种情况
This does not happen when I launch the application with spring-boot:run but only with the UnitTest
任何想法或建议将不胜感激.让我知道您是否需要更多信息或详细信息,或者我错过了重要事项.
Any ideas or suggest would be appreciated. Let me know if you'd need more infos or details or if I missed something important.
谢谢
我修复了使用@Autowired并使用此类注释来注释Mapper的问题:
I fixed the issues annotating the Mapper with @Autowired and using this class annotation:
@SpringBootTest(classes = {CandidateMapperImpl.class, UserMapperImpl.class, UserRolesMapperImpl.class,
CandidateMapper.class, UserMapper.class, UserRolesMapper.class})
概括:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SubMapperImpl.class, SubMapper.class,
OtherSubMapper.class, OtherSubMapperImpl.class...})
public class ServiceTest {
@Autowired
Mapper mapper;
...
}
推荐答案
基于带@RunWith(SpringRunner.class)
注释的测试(用于向测试中添加spring上下文),您似乎并不想嘲笑您的嵌套映射器,对吗?如果您希望使用Spring上下文,只需将其映射器及其所有嵌套的映射器自动连线即可.请参阅: https://stackoverflow.com/a/48503708/1098564
It seems, based on your test annotated with @RunWith(SpringRunner.class)
(which is for adding spring context to your test) that you don't really want to be mocking your nested mappers, correct? If you want spring context, just autowire your mapper with all its nested mappers as well. See: https://stackoverflow.com/a/48503708/1098564
下面(使用了Mockito ...还不像MockBean那样熟悉),将为您提供一个夹具"(即CandidateService),并注入所有嵌套的映射器(通过@Autowired),其余的依赖项都被模拟掉了. MockBean可能有一种更干净的方法,但目前没有时间对其进行全面测试.
Below (with mockito...not as familiar with MockBean yet), would give you a 'fixture' (i.e. CandidateService) with all the nested mappers injected (via @Autowired) and the rest of the dependencies are mocked out. There's probably a cleaner way with MockBean but don't currently have time to test it all out.
@RunWith(SpringRunner.class)
@SpringBootTest
public class CandidateServiceTest {
@Autowired
@InjectMocks
private CandidateService fixture;
@Mock
private UserRepository userRepository;
@Mock
private CandidateRepository candidateRepository;
@Mock
private UserDetailsService userDetailsService;
@Autowired
private CandidateMapper candidateMapper;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
//....tests
}
这篇关于JUnit-如何模拟MapStruct嵌套映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!