Mockito’s verify() checks whether a mock received an expected method call, including its arguments. In a money-transfer test, it checks that the repository updates both account balances; with never(), it can check that no update occurred. section 15.2
Listing 15.4 A unit test validating the happy flow
public class TransferServiceUnitTests {
// Omitted code
@Test
public void transferMoneyHappyFlow() {
Account sender = new Account();
sender.setId(1L);
sender.setAmount(new BigDecimal("1000"));
Account receiver = new Account();
receiver.setId(2L);
receiver.setAmount(new BigDecimal("1000"));
given(accountRepository.findById(1L))
.willReturn(Optional.of(sender));
given(accountRepository.findById(2L))
.willReturn(Optional.of(receiver));
transferService.transferMoney(1L, 2L, new BigDecimal("100"));
verify(accountRepository) #A
.changeAmount(1L, new BigDecimal("900")); #A
verify(accountRepository) #A
.changeAmount(2L, new BigDecimal("1100")); #A
}
}See verify() check the repository calls and their expected account IDs and balances after a transfer. section 15.2
