/
HasToStringTest.java
55 lines (44 loc) · 1.7 KB
/
HasToStringTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.hamcrest.object;
import org.hamcrest.Matcher;
import org.junit.Test;
import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.object.HasToString.hasToString;
public final class HasToStringTest {
private static final String TO_STRING_RESULT = "toString result";
private static final Object TEST_OBJECT = new Object() {
@Override
public String toString() {
return TO_STRING_RESULT;
}
};
@Test public void
copesWithNullsAndUnknownTypes() {
Matcher<Object> matcher = hasToString(equalTo("irrelevant"));
assertNullSafe(matcher);
assertUnknownTypeSafe(matcher);
}
@Test public void
matchesWhenUtilisingANestedMatcher() {
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
assertMatches(matcher, TEST_OBJECT);
assertDoesNotMatch(matcher, new Object());
}
@Test public void
matchesWhenUsingShortcutForHasToStringEqualTo() {
final Matcher<Object> matcher = hasToString(TO_STRING_RESULT);
assertMatches(matcher, TEST_OBJECT);
assertDoesNotMatch(matcher, new Object());
}
@Test public void
describesItself() {
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
assertDescription("with toString() \"toString result\"", matcher);
}
@Test public void
describesAMismatch() {
final Matcher<Object> matcher = hasToString(equalTo(TO_STRING_RESULT));
String expectedMismatchString = "toString() was \"Cheese\"";
assertMismatchDescription(expectedMismatchString, matcher, "Cheese");
}
}