Class HasPropertyWithValue<T>
- Type Parameters:
T
- the Matcher type
- All Implemented Interfaces:
Matcher<T>
,SelfDescribing
A matcher that checks if an object has a JavaBean property with the specified name and an expected value. 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 https://docs.oracle.com/javase/8/docs/technotes/guides/beans/index.html for more information on JavaBeans.
-
Constructor Summary
ConstructorDescriptionHasPropertyWithValue
(String propertyName, Matcher<?> valueMatcher) Constructor, best called fromhasProperty(String, Matcher)
orhasPropertyAtPath(String, Matcher)
.HasPropertyWithValue
(String propertyName, Matcher<?> valueMatcher, String messageFormat) Constructor, best called fromhasProperty(String, Matcher)
orhasPropertyAtPath(String, Matcher)
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
describeTo
(Description description) Generates a description of the object.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.static <T> Matcher
<T> hasPropertyAtPath
(String path, Matcher<T> valueMatcher) Creates a matcher that matches when the examined object is a graph of JavaBean objects that can be navigated along the declared dot-separated path and the final element of that path is a JavaBean property whose value satisfies the specified matcher.boolean
matchesSafely
(T bean, Description mismatch) Subclasses should implement this.Methods inherited from class org.hamcrest.TypeSafeDiagnosingMatcher
describeMismatch, matches
Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, isNotNull, toString
-
Constructor Details
-
HasPropertyWithValue
Constructor, best called fromhasProperty(String, Matcher)
orhasPropertyAtPath(String, Matcher)
.- Parameters:
propertyName
- the name of the propertyvalueMatcher
- matcher for the expected value
-
HasPropertyWithValue
Constructor, best called fromhasProperty(String, Matcher)
orhasPropertyAtPath(String, Matcher)
.- Parameters:
propertyName
- the name of the propertyvalueMatcher
- matcher for the expected valuemessageFormat
- format string for the description
-
-
Method Details
-
matchesSafely
Description copied from class:TypeSafeDiagnosingMatcher
Subclasses should implement this. The item will already have been checked for the specific type and will never be null.- Specified by:
matchesSafely
in classTypeSafeDiagnosingMatcher<T>
- Parameters:
bean
- the item.mismatch
- the mismatch description.- Returns:
- boolean true/false depending if item matches matcher.
-
describeTo
Description copied from interface:SelfDescribing
Generates a description of the object. The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.- Parameters:
description
- The description to be built or appended to.
-
hasProperty
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"))
- Type Parameters:
T
- the matcher type.- Parameters:
propertyName
- the name of the JavaBean property that examined beans should possessvalueMatcher
- a matcher for the value of the specified property of the examined bean- Returns:
- The matcher.
-
hasPropertyAtPath
Creates a matcher that matches when the examined object is a graph of JavaBean objects that can be navigated along the declared dot-separated path and the final element of that path is a JavaBean property whose value satisfies the specified matcher. For example:assertThat(myBean, hasProperty("foo.bar.baz", equalTo("a property value"))
- Type Parameters:
T
- the matcher type.- Parameters:
path
- the dot-separated path from the examined object to the JavaBean propertyvalueMatcher
- a matcher for the value of the specified property of the examined bean- Returns:
- The matcher.
-