Estou escrevendo o teste de um método findByID():
public DoctorDTO getDoctorById(long id) throws DoctorNotFoundException {
Optional<Doctor> doctorO = VerifyIfExists(id);
return doctorMapper.toDTO(doctorO.get());
}
private Optional<Doctor> VerifyIfExists(long id) throws DoctorNotFoundException {
Optional<Doctor> doctorO = doctorRepository.findById(id);
if (doctorO.isEmpty()) {
throw new DoctorNotFoundException(id);
}
return doctorO;
}
Como podem ver, eu uso um Mapper para retornar um tipo DoctorDTO.
No meu teste, usando o mockito:
@Mock
DoctorRepository doctorRepository;
@Mock
DoctorMapper doctorMapper;
@InjectMocks
DoctorService doctorService;
@Test
void testGivenValidDoctorThenReturnThisDoctor() throws DoctorNotFoundException {
DoctorDTO expectedDoctorDTO = createFakeDoctorDTO();
Doctor expectedDoctor = createFakeEntity();
when(doctorRepository.findById(expectedDoctor.getId())).thenReturn(Optional.of(expectedDoctor));
when(doctorMapper.toDTO(expectedDoctor)).thenReturn(expectedDoctorDTO);
DoctorDTO doctorDTO = doctorService.getDoctorById(expectedDoctor.getId());
assertEquals(expectedDoctorDTO, doctorDTO);
assertEquals(expectedDoctorDTO.getCrm(), doctorDTO.getCrm());
assertEquals(expectedDoctorDTO.getName(), doctorDTO.getName());
}
Me é retornado que when(doctorMapper.toDTO(expectedDoctor)).thenReturn(expectedDoctorDTO);
não é utilizado, porém o meu método (doctorService.getDoctorByID) utiliza doctorMapper.toDTO. Alguém saberia dizer por que a mensagem seguinte aparece quando rodo o teste ?
Unnecessary stubbings detected. Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary (click to navigate to relevant line of code): 1. -> at com.example.medicalrecordapi.services.DoctorServiceTest.testGivenValidDoctorThenReturnThisDoctor(DoctorServiceTest.java:61) Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
Estou usando o Mockito de forma errada?