I am just starting with Mockito and I just want to do something like :
public class Test {
public void clearList(List l){
doVeryLOOOONGDatabaseCallll();
l.clear();
return;
}
}
/// ...
Test test = mock(Test.class);
Mockito.when(test.clearList(any(List.class))).then( l => l.clear());
Have some hint to do the trick? Thank you for your help!
Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it.
Mockito when() method It should be used when we want to mock to return specific values when particular methods are called. In simple terms, "When the XYZ() method is called, then return ABC." It is mostly used when there is some condition to execute. Following code snippet shows how to use when() method: when(mock.
Simple Spy Example Let's start with a simple example of how to use a spy. Simply put, the API is Mockito. spy() to spy on a real object. This will allow us to call all the normal methods of the object while still tracking every interaction, just as we would with a mock.
Something like this should do it (not tested):
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
List<?> list = (List<?>) args[0];
list.clear();
return null;
}
}).when(test).clearList(any(List.class));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With