I have a StudentDetailMapper
:
@Mapper(componentModel = "spring", uses = SpouseDetailMapper.class)
public interface StudentDetailMapper {
StudentDetailMapper INSTANCE = Mappers.getMapper(StudentDetailMapper.class);-
StudentDetailDTO toDTO(StudentDetail studentDetail);
@InheritInverseConfiguration
StudentDetail fromDTO(StudentDetailDTO studentDetailDTO);}
I want to write a test for StudentDetailService
:
@ExtendWith(MockitoExtension.class)
class StudentDetailServiceTest {
@Mock
private StudentDetailRepository studentDetailRepository;
// ???
// @InjectMocks
// private SpouseDetailMapper spouseDetailMapper = Mappers.getMapper(SpouseDetailMapper.class);
@Spy
private StudentDetailMapper studentDetailMapper = Mappers.getMapper(StudentDetailMapper.class);
@InjectMocks
private StudentDetailService studentDetailService;
@Test
void getStudentDetail() {
when(studentDetailRepository.findByStudentStudentID(anyString())).thenReturn(Optional.of(studentDetail));
// when(spouseDetailMapper.toDTO(spouseDetail)).thenReturn(spouseDetailDTO);
when(studentDetailMapper.toDTO(studentDetail)).thenReturn(studentDetailDTO);
StudentDetailDTO result = studentDetailService.getStudentDetail(studentWithDetailID);
assertThat(studentDetailDTO).isEqualTo(result);
}
I know that you can write test that has MapStruct with @Spy
but if there's a submapper it will throw this error:
java.lang.NullPointerException: Cannot invoke "com.ex.project.mappers.SpouseDetailMapper.toDTO(com.ex.project.model.SpouseDetail)" because "this.spouseDetailMapper" is null