For Custom Matchers of a each of the above beans TypeSafeMatcher is being used, which takes the Type to be matched as the parameter, hence Type Safety is guranteed, hence no possible cases of ClassCastException could arise.
The AddressMatcher.java
public class AddressMatcher {
public static Matcher equalTo(final Address expectedAddress)
{
return new TypeSafeMatcher() {
@Override
public void describeTo(final Description description) {
description.appendText("expected result from getIsbn(): ")
.appendValue(expectedAddress);
}
@Override
public boolean matchesSafely(final Address actualAddress) {
return actualAddress.getHouseNo().equals(expectedAddress.getHouseNo()) && actualAddress.getLocality().equals(expectedAddress.getLocality()) && actualAddress.getCity().equals(expectedAddress.getCity()) ;
}
@Override
public void describeMismatchSafely(final Address address,
final Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(
address);
}
};
}
}
In the AddressMatcher.java, the we have provided an implementation of the equalTo() matcher.
The PersonMatcher.java
public class PersonMatcher {
public static Matcher equalTo(final Person expectedPerson)
{
return new TypeSafeMatcher() {
@Override
public void describeTo(final Description description) {
description.appendText("expected result from getIsbn(): ")
.appendValue(expectedPerson);
}
@Override
public boolean matchesSafely(final Person actualPerson) {
return actualPerson.getSsn() == expectedPerson.getSsn();
}
@Override
public void describeMismatchSafely(final Person person,
final Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(
person);
}
};
}
public static Matcher hasName(final String expectedPersonName)
{
System.out.println(" **** In HasName **** ");
return new TypeSafeMatcher() {
@Override
public void describeTo(final Description description) {
description.appendText("expected result from hasName(): ")
.appendValue(expectedPersonName);
}
@Override
public boolean matchesSafely(final Person actualPerson) {
return actualPerson.getName().equals(expectedPersonName);
}
@Override
public void describeMismatchSafely(final Person person,
final Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(
person.getName());
}
};
}
public static Matcher hasAddress(final String expectedAddress)
{
System.out.println(" **** In hasAddress **** ");
return new TypeSafeMatcher() {
@Override
public void describeTo(final Description description) {
description.appendText("expected result from hasAddress(): ")
.appendValue(expectedAddress);
}
@Override
public boolean matchesSafely(final Person actualPerson) {
return actualPerson.getAddress().getCity().equalsIgnoreCase(expectedAddress);
}
@Override
public void describeMismatchSafely(final Person person,
final Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(
person.getName());
}
};
}
}
In the PersonMatcher.java, we have provided equalTo(), hasName() and hasAddress() matchers.
The CompanyMatcher.java
public class CompanyMatcher {
public static Matcher hasCompanyName(final String expectedCompanyName)
{
return new TypeSafeMatcher() {
@Override
public void describeTo(final Description description) {
description.appendText("expected result from getIsbn(): ")
.appendValue(expectedCompanyName);
}
@Override
public boolean matchesSafely(final Company actualCompany) {
return actualCompany.getName().equals(expectedCompanyName);
}
@Override
public void describeMismatchSafely(final Company company,
final Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(
company.getName());
}
};
}
}
Similarly in the CompanyMatcher.java, there is hasCompanyName() matcher.
So this is in short about Custom Hamcrest Matcher.
No comments:
Post a Comment