Error in using Mockito. Actually, there Were zero interactions with this mock

Asked

Viewed 24 times

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);

    }
}

1 answer

1


You have to call the when before to call the method being tested:

Mockito.when(dao.obterAtrasados()).thenReturn(pendentes);
service.notificarLocacaoEmAtraso();

Mockito.verify(email).enviarEmailCobranca(pedro);

The when defines what the obterAtrasados should return. But as you were already making the call to notificarLocacaoEmAtraso before, and this flame obterAtrasados (i.e., first executed the method, then defined what it should return), it ended up not returning the value pendentes that you defined. With this, the returned list was empty and it did not enter the loop, and therefore no call from enviarEmailCobranca was made (hence the error of "zero interactions with this mock").

Calling when before, you have the expected behavior. Already the call of verify continues to be later, because then it serves to verify whether or not something was executed.


In general, you call when to define what mocks specific will return, and must do so before the method being tested. And verify is used after the method being tested, to see if everything you want to actually check happened.

  • 1

    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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.