Testing a void method with mockito

Asked

Viewed 5,014 times

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?

3 answers

4

In fact the mockite does not serve to test, it serves to create mocks of objects and methods. When I say it’s not for testing, I mean it won’t validate if the method worked as expected. So you actually want to know one of these two things:

  1. How to test a void method.
  2. How to create a mock of a void method.

How to test a void method

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

  1. Change the class status. -> In this case, to know if the method worked as expected, you will check if the class was in the expected state.

  2. Change the status of the bank. -> In the same way as in the previous item, the test should check whether the bank was in the expected state.

  3. Generation of some output. -> You will check whether the output was generated and whether it was generated as expected.

  4. Integration with another system. -> The integration will probably provide some way you can validate whether the method ran as expected. In this case, the way to test will depend on how the integration is done and what is available.

How to create a mock of a void method

  1. Use the doAnswer.

Imagining that you have this method:

public class App {

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

You can do it 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
}

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

  1. Using the doNothing.

-

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

doNohting will make the method do nothing when executed.

1

Since the method is void, it is not necessary to set up a return, so you just need to ensure that this method should be called, if it should not be called, or how often it should be called. This is done through the concept of Verify, take as an example the class below.

class Foo {

   public void foo() {
      //
   }

}

then to check whether the method has been called (once):

Foo mockFoo = mock(Foo.class);

verify(mockFoo).foo();

If it was called more than once (in the case below 2x):

verify(mockFoo, times(2)).foo();

If the void method should not be called:

verify(mockFoo, never()).foo();

The ideal is always to consult the mockite documentation which is very good in my opinion: http://static.javadoc.io/org.mockito/mockito-core/2.24.0/org/mockito/Mockito.html#4

0

Browser other questions tagged

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