HasProperty extends TypeSafeMatcher<T> { public HasProperty(String propertyName) { this.propertyName = propertyName; } HasProperty(String propertyName); @Override boolean matchesSafely(T obj); @Override void describeMismatchSafely(T item, Description mismatchDescription); @Override void describeTo(Description description); static Matcher<T> hasProperty(String propertyName);  }
@Test public void copesWithNullsAndUnknownTypes() { Matcher<Object> matcher = hasProperty("irrelevant"); assertNullSafe(matcher); assertUnknownTypeSafe(matcher); }
@Test public void matchesWhenThePropertyExists() { assertMatches(hasProperty("writeOnlyProperty"), bean); }
@Test public void doesNotMatchIfPropertyDoesNotExist() { assertDoesNotMatch(hasProperty("aNonExistentProp"), bean); }
@Test public void describesItself() { assertDescription("hasProperty(\"property\")", hasProperty("property")); }
@Test public void describesAMismatch() { assertMismatchDescription("no \"aNonExistentProp\" in <[Person: a bean]>", hasProperty("aNonExistentProp"), bean); }