Skip to content

Commit 79aa25d

Browse files
committed
convert to JUnit4
1 parent fe36747 commit 79aa25d

1 file changed

Lines changed: 36 additions & 27 deletions

File tree

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,60 @@
11
package org.hamcrest.object;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
5+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
6+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
7+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
8+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
49
import static org.hamcrest.core.IsEqual.equalTo;
5-
import static org.hamcrest.core.IsNot.not;
610
import static org.hamcrest.object.HasToString.hasToString;
711

8-
import org.hamcrest.AbstractMatcherTest;
912
import org.hamcrest.Matcher;
10-
import org.hamcrest.StringDescription;
13+
import org.junit.Test;
1114

12-
public class HasToStringTest extends AbstractMatcherTest {
15+
public final class HasToStringTest {
1316
private static final String TO_STRING_RESULT = "toString result";
14-
private static final Object ARG = new Object() {
17+
private static final Object TEST_OBJECT = new Object() {
1518
@Override
1619
public String toString() {
1720
return TO_STRING_RESULT;
1821
}
1922
};
2023

21-
@Override
22-
protected Matcher<?> createMatcher() {
23-
return hasToString(equalTo("irrelevant"));
24+
@Test public void
25+
copesWithNullsAndUnknownTypes() {
26+
Matcher<Object> matcher = hasToString(equalTo("irrelevant"));
27+
28+
assertNullSafe(matcher);
29+
assertUnknownTypeSafe(matcher);
2430
}
31+
32+
@Test public void
33+
matchesWhenUtilisingANestedMatcher() {
34+
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
2535

26-
public void testPassesResultOfToStringToNestedMatcher() {
27-
assertThat(ARG, hasToString(equalTo(TO_STRING_RESULT)));
28-
assertThat(ARG, not(hasToString(equalTo("OTHER STRING"))));
36+
assertMatches("didn't match", matcher, TEST_OBJECT);
37+
assertDoesNotMatch("matched unexpectedly", matcher, new Object());
2938
}
3039

31-
public void testProvidesConvenientShortcutForHasToStringEqualTo() {
32-
assertThat(ARG, hasToString(TO_STRING_RESULT));
33-
assertThat(ARG, not(hasToString("OTHER STRING")));
40+
@Test public void
41+
matchesWhenUsingShortcutForHasToStringEqualTo() {
42+
final Matcher<Object> matcher = hasToString(TO_STRING_RESULT);
43+
44+
assertMatches("didn't match", matcher, TEST_OBJECT);
45+
assertDoesNotMatch("matched unexpectedly", matcher, new Object());
3446
}
3547

36-
public void testHasReadableDescription() {
37-
Matcher<? super String> toStringMatcher = equalTo(TO_STRING_RESULT);
38-
Matcher<Matcher<String>> matcher = hasToString(toStringMatcher);
39-
40-
assertEquals("with toString() " + descriptionOf(toStringMatcher), descriptionOf(matcher));
41-
}
42-
43-
public void testMismatchContainsToStringValue() {
44-
String expectedMismatchString = "toString() was \"Cheese\"";
45-
assertMismatchDescription(expectedMismatchString, hasToString(TO_STRING_RESULT), "Cheese");
48+
@Test public void
49+
describesItself() {
50+
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
51+
assertDescription("with toString() \"toString result\"", matcher);
4652
}
4753

48-
private static String descriptionOf(Matcher<?> matcher) {
49-
return StringDescription.asString(matcher);
54+
@Test public void
55+
describesAMismatch() {
56+
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
57+
String expectedMismatchString = "toString() was \"Cheese\"";
58+
assertMismatchDescription(expectedMismatchString, matcher, "Cheese");
5059
}
5160
}

0 commit comments

Comments
 (0)