liveBook · Manning

Never() verification mode

As explained in Spring Start Here, Second Edition

The Mockito never() method returns a verification mode that checks that a mock method was not called.

An exception can be thrown even after an unintended update. Mockito’s never() verification mode checks that a specified method on a mock was not called. section 15.2

Prevent updates after a transfer error

When a transfer’s destination account is missing, assert that the call throws AccountNotFoundException, then verify(accountRepository, never()).changeAmount(anyLong(), any()). The test fails if changeAmount() was called. section 15.2

How Spring Start Here, Second Edition covers it

How deep it goes
Defines never() as a verification mode and demonstrates it with repository calls.
Examples
Uses a missing destination account in a money-transfer test to check that repository updates never run.
What you can do after
Apply it via Mockito to fail a unit test when a specified mock method is called.
How it connects
Connects Mockito verification with mock interactions, exception testing, and assertThrows().

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.