Skip to content

Commit a5a9090

Browse files
author
nat.pryce
committed
Added optional description to assertThat to make error messages clearer when tests have more than one assertion
1 parent c77c2af commit a5a9090

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="hamcrest-api/src/main/java"/>
4+
<classpathentry kind="src" path="build/generated-code"/>
45
<classpathentry kind="src" path="hamcrest-generator/src/main/java"/>
56
<classpathentry kind="src" path="hamcrest-examples/src/main/java"/>
67
<classpathentry kind="src" path="hamcrest-integration/src/main/java"/>
78
<classpathentry kind="src" path="hamcrest-library/src/main/java"/>
89
<classpathentry kind="src" path="hamcrest-unit-test/src/main/java"/>
910
<classpathentry kind="lib" path="lib/integration/easymock-2.2.jar"/>
1011
<classpathentry kind="lib" path="lib/integration/jmock-1.10RC1.jar"/>
11-
<classpathentry kind="lib" path="lib/integration/junit-3.8.1.jar"/>
1212
<classpathentry kind="lib" path="lib/integration/junit-4.0.jar"/>
1313
<classpathentry kind="lib" path="lib/integration/testng-4.6-jdk15.jar"/>
1414
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>

hamcrest-integration/src/main/java/org/hamcrest/MatcherAssert.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55

66
public class MatcherAssert {
7-
87
public static <T> void assertThat(T actual, Matcher<T> matcher) {
8+
assertThat("", actual, matcher);
9+
}
10+
11+
public static <T> void assertThat(String name, T actual, Matcher<T> matcher) {
912
if (!matcher.matches(actual)) {
1013
Description description = new StringDescription();
14+
description.appendText(name);
1115
description.appendText("\nExpected: ");
1216
matcher.describeTo(description);
13-
description.appendText("\n got : ").appendValue(actual).appendText("\n");
17+
description.appendText("\n got: ").appendValue(actual).appendText("\n");
1418
throw new java.lang.AssertionError(description.toString());
1519
}
1620
}
17-
1821
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hamcrest;
2+
3+
import junit.framework.TestCase;
4+
import static org.hamcrest.Matchers.*;
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
7+
public class MatcherAssertTest extends TestCase {
8+
public void testIncludesDescriptionOfTestedValueInErrorMessage() {
9+
String expected = "expected";
10+
String actual = "actual";
11+
12+
String expectedMessage = "identifier\nExpected: \"expected\"\n got: \"actual\"\n";
13+
14+
try {
15+
assertThat("identifier", actual, equalTo(expected));
16+
}
17+
catch (AssertionError e) {
18+
assertEquals(expectedMessage, e.getMessage());
19+
}
20+
}
21+
22+
public void testDescriptionCanBeElided() {
23+
String expected = "expected";
24+
String actual = "actual";
25+
26+
String expectedMessage = "\nExpected: \"expected\"\n got: \"actual\"\n";
27+
28+
try {
29+
assertThat(actual, equalTo(expected));
30+
}
31+
catch (AssertionError e) {
32+
assertEquals(expectedMessage, e.getMessage());
33+
}
34+
}
35+
}
36+

0 commit comments

Comments
 (0)