Verifying that interactions stopped happening
In this recipe, we will verify that a specified method on a mock was executed and then any interactions stopped taking place.
Getting ready
For this recipe, our system under test will be a TaxTransferer
class that will transfer tax for a non-null person. If the passed person value is null, then an error report is sent:
public class TaxTransferer { private final TaxService taxService; public TaxTransferer(TaxService taxService) { this.taxService = taxService; } public void transferTaxFor(Person person) { if (person == null) { taxService.sendErrorReport(); return; } taxService.transferTaxFor(person); } }
How to do it...
To verify that the only method executed on a mock is the one provided by us, you have to call Mockito.verify(mock, VerificationMode.only()).methodToVerify(...)
.
Let's check the JUnit test that verifies whether the web service's method has been called at most...