When to use Mockito.verify()?

I write jUnit test cases for 3 purposes:

  1. To ensure that my code satisfies all of the required functionality, under all (or most of) the input combinations/values.
  2. To ensure that I can change the implementation, and rely on JUnit test cases to tell me that all my functionality is still satisfied.
  3. As a documentation of all the use cases my code handles, and act as a spec for refactoring – should the code ever need to be rewritten. (Refactor the code, and if my jUnit tests fail – you probably missed some use case).

I do not understand why or when Mockito.verify() should be used. When I see verify() being called, it is telling me that my jUnit is becoming aware of the implementation. (Thus changing my implementation would break my jUnits, even though my functionality was unaffected).

I’m looking for:

  1. What should be the guidelines for appropriate usage of Mockito.verify()?

  2. Is it fundamentally correct for jUnits to be aware of, or tightly coupled to, the implementation of the class under test?

6 Answers
6

Leave a Comment