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
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?
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:
How to test a void method
If the method is void, then probably its execution will reflect somewhere in some way. Some possibilities:
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.
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.
Generation of some output. -> You will check whether the output was generated and whether it was generated as expected.
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
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.
-
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
Hi! When you are using mockite, you define how your scenario will be. In this example you quoted you are defining what will happen.
Now, if you want to validate whether calls within your void method have been executed, you can use Mockito.Verify(...) for example. See in the documentation: http://site.mockito.org/mockito/docs/1.10.19/org/mockito/Mockito.html#Verification
I hope I helped you.
Browser other questions tagged java unit-testing mockito
You are not signed in. Login or sign up in order to post.