11package org .hamcrest ;
22
3- import static org .hamcrest .AbstractMatcherTest .*;
4- import static org .hamcrest .Matchers .equalTo ;
3+ import static org .hamcrest .AbstractMatcherTest .assertDescription ;
4+ import static org .hamcrest .AbstractMatcherTest .assertMatches ;
5+ import static org .hamcrest .AbstractMatcherTest .assertMismatchDescription ;
56import junit .framework .TestCase ;
67
8+ import org .hamcrest .core .IsEqual ;
9+
710public class FeatureMatcherTest extends TestCase {
811 private final FeatureMatcher <Thingy , String > resultMatcher = resultMatcher ();
912
10- public void testMatchesPartOfAnObject () {
11- assertMatches ("feature" , resultMatcher , new Thingy ("bar" ));
12- assertDescription ("Thingy with result \" bar\" " , resultMatcher );
13- }
13+ public void testMatchesPartOfAnObject () {
14+ assertMatches ("feature" , resultMatcher , new Thingy ("bar" ));
15+ assertDescription ("Thingy with result \" bar\" " , resultMatcher );
16+ }
17+
18+ public void testMismatchesPartOfAnObject () {
19+ assertMismatchDescription ("result was mismatch-description" , resultMatcher , new Thingy ("foo" ));
20+ }
21+
22+ public void testDoesNotThrowNullPointerException () {
23+ assertMismatchDescription ("was null" , resultMatcher , null );
24+ }
25+
26+ public void testDoesNotThrowClassCastException () {
27+ resultMatcher .matches (new ShouldNotMatch ());
28+ StringDescription mismatchDescription = new StringDescription ();
29+ resultMatcher .describeMismatch (new ShouldNotMatch (), mismatchDescription );
30+ assertEquals ("was <ShouldNotMatch>" , mismatchDescription .toString ());
31+ }
1432
15- public void testMismatchesPartOfAnObject () {
16- assertMismatchDescription ("result was \" foo\" " , resultMatcher , new Thingy ("foo" ));
33+
34+ public static class Match extends IsEqual <String > {
35+ public Match (String equalArg ) { super (equalArg ); }
36+ @ Override public void describeMismatch (Object item , Description description ) {
37+ description .appendText ("mismatch-description" );
1738 }
18-
19- public void testDoesNotThrowNullPointerException () {
20- assertMismatchDescription ("was null" , resultMatcher , null );
21- }
22-
23- public void testDoesNotThrowClassCastException () {
24- resultMatcher .matches (new ShouldNotMatch ());
25- StringDescription mismatchDescription = new StringDescription ();
26- resultMatcher .describeMismatch (new ShouldNotMatch (), mismatchDescription );
27- assertEquals ("was <ShouldNotMatch>" , mismatchDescription .toString ());
28- }
29-
39+ }
3040 public static class Thingy {
3141 private final String result ;
3242
@@ -44,7 +54,7 @@ public static class ShouldNotMatch {
4454 }
4555
4656 private static FeatureMatcher <Thingy , String > resultMatcher () {
47- return new FeatureMatcher <Thingy , String >(equalTo ("bar" ), "Thingy with result" , "result" ) {
57+ return new FeatureMatcher <Thingy , String >(new Match ("bar" ), "Thingy with result" , "result" ) {
4858 @ Override
4959 public String featureValueOf (Thingy actual ) {
5060 return actual .getResult ();
0 commit comments