verify() checks that a specific interaction happened on a mock, while verifyNoMoreInteractions() checks that nothing beyond the interactions you've already verified occurred on that mock.
Key Points: • verify(mock).method(args) targets one particular call and its arguments/count. • verifyNoMoreInteractions(mock) is called after all your verify() calls, to assert there are no leftover, unverified interactions. • verifyNoMoreInteractions() forces you to explicitly account for every call made to a mock, which can make tests more precise but also more brittle. • It should not be used on every mock in every test - overuse tightly couples tests to exact interaction sequences and makes refactoring painful. • verifyNoInteractions() is a related but different method that checks a mock was never called at all.
Example: After verifying that a repository's save() was called, adding verifyNoMoreInteractions(repository) ensures the code under test didn't also unexpectedly call delete() or update() on the same mock.
Code Example:
verify(repository).save(order);
verifyNoMoreInteractions(repository);Interview Tip: A concise interview answer is:
"verify() confirms a specific call happened with expected arguments, while verifyNoMoreInteractions() confirms nothing else happened on that mock beyond what I've already verified. I use the latter sparingly, since it can make tests overly strict about interaction order and count."