|
6 | 6 | import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
|
7 | 7 | import static org.hamcrest.core.IsAnything.anything;
|
8 | 8 | import static org.hamcrest.core.IsEqual.equalTo;
|
9 |
| -import static org.hamcrest.core.IsNot.not; |
10 | 9 |
|
11 | 10 | import java.beans.IntrospectionException;
|
12 | 11 | import java.beans.PropertyDescriptor;
|
@@ -38,45 +37,43 @@ protected Matcher<?> createMatcher() {
|
38 | 37 | }
|
39 | 38 |
|
40 | 39 | public void testMatchesInfolessBeanWithMatchedNamedProperty() {
|
41 |
| - assertThat(shouldMatch, hasProperty("property", equalTo("is expected"))); |
42 |
| - assertThat(shouldNotMatch, not(hasProperty("property", equalTo("is expected")))); |
| 40 | + assertMatches("with property", hasProperty("property", equalTo("is expected")), shouldMatch); |
| 41 | + assertMismatchDescription("property \"property\" was \"not expected\"", |
| 42 | + hasProperty("property", equalTo("is expected")), shouldNotMatch); |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
|
46 |
| - assertThat(beanWithInfo, hasProperty("property", equalTo("with info"))); |
| 46 | + assertMatches("with bean info", hasProperty("property", equalTo("with info")), beanWithInfo); |
| 47 | + assertMismatchDescription("property \"property\" was \"with info\"", |
| 48 | + hasProperty("property", equalTo("without info")), beanWithInfo); |
47 | 49 | }
|
48 | 50 |
|
49 | 51 | public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
|
50 |
| - assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty", anything()))); |
51 |
| - } |
| 52 | + assertMismatchDescription("No property \"nonExistentProperty\"", |
| 53 | + hasProperty("nonExistentProperty", anything()), shouldNotMatch); |
| 54 | + } |
52 | 55 |
|
53 | 56 | public void testDoesNotMatchWriteOnlyProperty() {
|
54 |
| - assertThat(shouldNotMatch, |
55 |
| - not(hasProperty("writeOnlyProperty", anything()))); |
| 57 | + assertMismatchDescription("property \"writeOnlyProperty\" is not readable", |
| 58 | + hasProperty("writeOnlyProperty", anything()), shouldNotMatch); |
56 | 59 | }
|
57 | 60 |
|
58 | 61 | public void testDescribeTo() {
|
59 |
| - Matcher<?> matcher = equalTo(true); |
60 |
| - |
61 |
| - assertDescription("hasProperty(\"property\", " |
62 |
| - + StringDescription.asString(matcher) + ")", hasProperty("property", matcher)); |
| 62 | + assertDescription("hasProperty(\"property\", <true>)", hasProperty("property", equalTo(true))); |
63 | 63 | }
|
64 | 64 |
|
| 65 | + public void testMatchesPropertyAndValue() { |
| 66 | + assertMatches("property with value", hasProperty( "property", anything()), beanWithInfo); |
| 67 | + } |
| 68 | + |
65 | 69 | public void testDoesNotWriteMismatchIfPropertyMatches() {
|
66 |
| - Matcher<Object> matcher = hasProperty( "property", anything()); |
67 |
| - assertTrue("Precondtion: Matcher should match item.", matcher.matches(beanWithInfo)); |
68 | 70 | Description description = new StringDescription();
|
69 |
| - matcher.describeMismatch(beanWithInfo, description); |
| 71 | + hasProperty( "property", anything()).describeMismatch(beanWithInfo, description); |
70 | 72 | assertEquals("Expected mismatch description", "", description.toString());
|
71 | 73 | }
|
72 | 74 |
|
73 | 75 | public void testDescribesMissingPropertyMismatch() {
|
74 |
| - assertMismatchDescription("missing.", hasProperty( "honk", anything()), shouldNotMatch); |
75 |
| - } |
76 |
| - |
77 |
| - public void testDescribesMismatchingPropertyValueMismatch() { |
78 |
| - assertMismatchDescription("was \"not expected\".", |
79 |
| - hasProperty("property", equalTo("foo")), shouldNotMatch); |
| 76 | + assertMismatchDescription("No property \"honk\"", hasProperty( "honk", anything()), shouldNotMatch); |
80 | 77 | }
|
81 | 78 |
|
82 | 79 | public void testCanAccessAnAnonymousInnerClass() {
|
@@ -133,8 +130,9 @@ public static class BeanWithInfoBeanInfo extends SimpleBeanInfo {
|
133 | 130 | @Override
|
134 | 131 | public PropertyDescriptor[] getPropertyDescriptors() {
|
135 | 132 | try {
|
136 |
| - return new PropertyDescriptor[] { new PropertyDescriptor("property", |
137 |
| - BeanWithInfo.class, "property", null) }; |
| 133 | + return new PropertyDescriptor[] { |
| 134 | + new PropertyDescriptor("property", BeanWithInfo.class, "property", null) |
| 135 | + }; |
138 | 136 | } catch (IntrospectionException e) {
|
139 | 137 | throw new RuntimeException("Introspection exception: " + e.getMessage());
|
140 | 138 | }
|
|
0 commit comments