Top Description Fields Constructors Methods
org.hamcrest.beans

public Class HasPropertyWithValue<T>

extends TypeSafeDiagnosingMatcher<T>
Class Inheritance
Imports
java.beans.PropertyDescriptor, java.lang.reflect.Method, org.hamcrest.Condition, .Description, .Factory, .Matcher, .TypeSafeDiagnosingMatcher

Matcher that asserts that a JavaBean property on an argument passed to the mock object meets the provided matcher. This is useful for when objects are created within code under test and passed to a mock object, and you wish to assert that the created object has certain properties.

Example Usage

Consider the situation where we have a class representing a person, which follows the basic JavaBean convention of having get() and possibly set() methods for it's properties:
public class Person {
  private String name;
  public Person(String person) {
    this.person = person;
  }
  public String getName() {
    return name;
  }
}
And that these person objects are generated within a piece of code under test (a class named PersonGenerator). This object is sent to one of our mock objects which overrides the PersonGenerationListener interface:
public interface PersonGenerationListener {
  public void personGenerated(Person person);
}
In order to check that the code under test generates a person with name "Iain" we would do the following:
Mock personGenListenerMock = mock(PersonGenerationListener.class);
personGenListenerMock.expects(once()).method("personGenerated").with(and(isA(Person.class), hasProperty("Name", eq("Iain")));
PersonGenerationListener listener = (PersonGenerationListener)personGenListenerMock.proxy();
If an exception is thrown by the getter method for a property, the property does not exist, is not readable, or a reflection related exception is thrown when trying to invoke it then this is treated as an evaluation failure and the matches method will return false.

This matcher class will also work with JavaBean objects that have explicit bean descriptions via an associated BeanInfo description class. See the JavaBeans specification for more information:

http://java.sun.com/products/javabeans/docs/index.html

Authors
Iain McGinniss, Nat Pryce, Steve Freeman

Field Summary

Modifier and TypeField and Description
private final String
private final Matcher<Object>
private static final Condition.Step<PropertyDescriptor, Method>

Constructor Summary

AccessConstructor and Description
public
HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher)

Method Summary

Modifier and TypeMethod and Description
public void
describeTo(Description
The description to be built or appended to.
description
)

Implements org.hamcrest.SelfDescribing.describeTo.

Generates a description of the object.
public static <T> Matcher<T>
hasProperty(String
the name of the JavaBean property that examined beans should possess
propertyName
,
Matcher<?>
a matcher for the value of the specified property of the examined bean
valueMatcher
)

Creates a matcher that matches when the examined object has a JavaBean property with the specified name whose value satisfies the specified matcher.

public boolean
matchesSafely(T bean, Description mismatch)

Implements abstract org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Subclasses should implement this.
private static Matcher<Object>
private Condition<PropertyDescriptor>
propertyOn(T bean, Description mismatch)

private Condition.Step<Method, Object>
withPropertyValue(final T bean)

private static Condition.Step<PropertyDescriptor, Method>
Inherited from org.hamcrest.TypeSafeDiagnosingMatcher:
describeMismatchmatches

Field Detail

propertyNameback to summary
private final String propertyName
valueMatcherback to summary
private final Matcher<Object> valueMatcher
WITH_READ_METHODback to summary
private static final Condition.Step<PropertyDescriptor, Method> WITH_READ_METHOD

Constructor Detail

HasPropertyWithValueback to summary
public HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher)

Method Detail

describeToback to summary
public void describeTo(Description description)

Implements org.hamcrest.SelfDescribing.describeTo.

Doc from org.hamcrest.SelfDescribing.describeTo.

Generates a description of the object. The description may be part of a a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters
description:Description

The description to be built or appended to.

Annotations
@Override
hasPropertyback to summary
public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> valueMatcher)

Creates a matcher that matches when the examined object has a JavaBean property with the specified name whose value satisfies the specified matcher.

For example:

assertThat(myBean, hasProperty("foo", equalTo("bar"))
Parameters
propertyName:String

the name of the JavaBean property that examined beans should possess

valueMatcher:Matcher<?>

a matcher for the value of the specified property of the examined bean

Annotations
@Factory
matchesSafelyback to summary
public boolean matchesSafely(T bean, Description mismatch)

Implements abstract org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Doc from org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Subclasses should implement this. The item will already have been checked for the specific type and will never be null.

Annotations
@Override
nastyGenericsWorkaroundback to summary
private static Matcher<Object> nastyGenericsWorkaround(Matcher<?> valueMatcher)
Annotations
@SuppressWarnings:unchecked
propertyOnback to summary
private Condition<PropertyDescriptor> propertyOn(T bean, Description mismatch)
withPropertyValueback to summary
private Condition.Step<Method, Object> withPropertyValue(final T bean)
withReadMethodback to summary
private static Condition.Step<PropertyDescriptor, Method> withReadMethod()