1
I am trying to do a check but for some reason the test is not passing. I have no idea why. Follow the error shown:
Actually, there Were zero interactions with this mock.
Follows the code:
public class LocacaoService {
private LocacaoDao dao;
private EmailService emailService;
public void notificarLocacaoEmAtraso(){
List<Locacao> locacaoesEmAtrado = dao.obterAtrasados();
for(Locacao locacao : locacaoesEmAtrado){
emailService.enviarEmailCobranca(locacao.getUsuario());
}
}
public void setDao(LocacaoDao dao) {
this.dao = dao;
}
public void setEmailService(EmailService emailService){
this.emailService = emailService;
}
}
public interface LocacaoDao {
void salvar(Locacao locacao);
List<Locacao> obterAtrasados();
}
public interface EmailService {
void enviarEmailCobranca(Usuario usuario);
}
The test I did that is presenting error:
public class LocacaoServiceTest {
private Usuario pedro;
private List<Filme> filmes;
private LocacaoService service;
private Locacao locacao;
private LocacaoDao dao;
private EmailService email;
@BeforeEach
public void criaUsuarioLocacaoELocacaoService() {
pedro = new Usuario("Pedro");
filmes = new ArrayList<>();
service = new LocacaoService();
locacao = new Locacao();
dao = Mockito.mock(LocacaoDao.class);
service.setDao(dao);
email = Mockito.mock(EmailService.class);
service.setEmailService(email);
}
@Test
public void deveEnviarEmailDeCobrança() {
Locacao locacao = new Locacao();
locacao.setUsuario(pedro);
locacao.setDataRetorno(DataUtils.obterDataComDiferencaDias(-2));
List<Locacao> pendentes = new ArrayList<>();
pendentes.add(locacao);
service.notificarLocacaoEmAtraso();
Mockito.when(dao.obterAtrasados()).thenReturn(pendentes);
Mockito.verify(email).enviarEmailCobranca(pedro);
}
}
Man, thank you so much! First for the answer grade 10 and second for the class. I never imagined that was the reason. Thank you!
– CaioAbreuDev