Creating custom Hamcrest matchers

In this recipe, we will create a custom Hamcrest matcher. Please refer to the previous recipe in terms of the presented assertions in the test because in the current recipe, we will combine them in our custom matchers.

Getting ready

For this recipe, our system under test will be a NewPersonGenerator class that will call an external service, NewIdentityCreator, to generate a new identity for the current person, as shown in the following code:

public class NewPersonGenerator { private final NewIdentityCreator newIdentityCreator; public NewPersonGenerator(NewIdentityCreator newIdentityCreator) { this.newIdentityCreator = newIdentityCreator; } public Person generateNewIdentity(Person person) { String newName = newIdentityCreator.createNewName(person); ...

Get Mockito Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.