Test a void method with mockito

3

How to test a void method with mockito?

This is the basic syntax for a return method

when(Classe.metodo()).thenReturn(variavelRetorno);

However, how do I test a void method?

    
asked by anonymous 08.07.2016 / 21:10

2 answers

2

In fact mockito is not for testing, it serves to create mocks of objects and methods. When I say that it is not for testing, I want to say that it will not validate if the method worked as expected. So you actually want to know one of these two things:

  • How to test a void method.
  • How to create a mock of a void method.
  • Testing a void method

    If the method is void, then probably its execution will reflect somewhere in some way. Some possibilities:

  • Change the state of the class. - > In this case, to find out if the method worked as expected, you will check if the class has been in the expected state.

  • Change the bank state. - > In the same way as in the previous item, the test should check if the bank was in the expected state.

  • Generating some output. - > You will check if the output was generated and whether it was generated as expected.

  • Integration with another system. - > The integration will likely provide some means that you can validate if the method performed as expected. In that case, the way you test will depend on how the integration is done and what is available.

  • How to create a mock of a void method

  • Use doAnswer.
  • Imagining that you have this method:

    public class App {
    
    public void salvar(Pessoa pessoa) {
        System.out.println("Salva no banco");
    }}
    

    You can do this here:

    @Test
    public void salvarTest() {
        App app = mock(App.class);
        Answer<Pessoa> answer = new Answer<Pessoa>() {
    
            @Override
            public Pessoa answer(InvocationOnMock invocation) throws Throwable {
                Pessoa pessoa = (Pessoa) invocation.getArguments()[0];
                pessoa.setId(1);
    
                System.out.printf("Salvando %s", pessoa.getNome());
                return null;
            }
        };
    
        Pessoa pessoa = new Pessoa("João");
        doAnswer(answer).when(app).salvar(pessoa);
        app.salvar(pessoa);
        //realizar teste
    }
    

    The doAnswer can be used when you want to perform some operation over the arguments passed in the method. It can be used with methods that have payback as well. The object that the answer returns will be returned. In that case, as the method is void the return could be anything.

  • Use doNothing.
  • -

    App app = mock(App.class);
    Pessoa pessoa = new Pessoa("João");
    doNothing().when(app).salvar(pessoa);
    

    doNohting will prevent the method from doing anything when it is executed.

        
    24.08.2016 / 23:06
    0

    Hi! When you are using mockito, you define what your scenario will look like. In this example you mentioned you are defining what will happen.

    Now, if you want to validate if the calls within your void method were executed, you can use Mockito.verify (...) for example. See the documentation: link

    I hope I have helped you.

        
    12.07.2016 / 14:57