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); int newAge = newIdentityCreator.createNewAge(person); List<Person> newSiblings = newIdentityCreator.createNewSiblings(person); return new Person(newName...