mockito Getting started with mockito Stubbing void methods


Example

void methods can be stubbed using the doThrow(), doAnswer(), doNothing(), doCallRealMethod() family of methods.

Runnable mock = mock(Runnable.class);

doThrow(new UnsupportedOperationException()).when(mock).run();

mock.run(); // throws the UnsupportedOperationException

Note thatvoid methods can't be stubbed using when(..) cause the compiler don't like void methods as argument.