@Incubating public interface VerificationStartedListener
VerificationStartedListenerTestclass. Mockito is Open Source so feel free to dive into the code!
How can you add listeners? The listener is attached to the mock object during creation:
When multiple listeners are added, they are notified in order. There is no reason to add multiple listeners but we wanted to keep the API simple and consistent with how we manage Mock object listeners. See
List mock = Mockito.mock(List.class, withSettings().verificationStartedListeners(myListener));
When is the listener notified?
//given verification: verify(mock).someMethod(); //let's slit it into 2 distinct steps so that it is easy to explain: //step 1 verify(mock); //step 2 mock.someMethod(); //the listener is notified during step 1 //step 2 is when Mockito attempts to verify the method call
What can I do when the listener is notified? The main reason we added this listener to the API is to allow to replace the mock object that is about to be verified. This is a pretty hardcore use case, needed by other frameworks that wrap Mockito with another layer of proxying. Such framework may need to unwrap the outer proxy layer and pass genuine Mockito mock to the verification. For specific use case how it is needed by Spring Boot, see issue 1191.
When do I use the listener? Unless you write a framework that integrates with Mockito, there is no reason for you to use this API. Keep mocking and writing great unit tests!