public class HasPropertyWithValue<T> extends TypeSafeDiagnosingMatcher<T>
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
Constructor and Description |
---|
HasPropertyWithValue(String propertyName,
Matcher<?> valueMatcher) |
Modifier and Type | Method and Description |
---|---|
void |
describeTo(Description description)
Generates a description of the object.
|
static <T> Matcher<T> |
hasProperty(String propertyName,
Matcher<?> value) |
boolean |
matchesSafely(T bean,
Description mismatchDescription)
Subclasses should implement this.
|
describeMismatch, matches
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
public boolean matchesSafely(T bean, Description mismatchDescription)
TypeSafeDiagnosingMatcher
matchesSafely
in class TypeSafeDiagnosingMatcher<T>
public void describeTo(Description description)
SelfDescribing
description
- The description to be built or appended to.Copyright © 2016. All rights reserved.