Permalink
Browse files

assertThat uses org.hamcrest.MatcherAssert

As pointed out by Steve Freeman of Hamcrest, the Matcher interface now
has an additional method describeMismatch. To be safe to catch such 
improvements in the future, MatcherAssert is used instead of
duplicating its implementation.
  • Loading branch information...
marcphilipp committed Apr 3, 2012
1 parent 8b9dfef commit a0eea1357f0a0f4faea1245197f20ceee5ca3a5a
@@ -1,8 +1,7 @@
package org.junit;
-import org.hamcrest.Description;
import org.hamcrest.Matcher;
-import org.hamcrest.StringDescription;
+import org.hamcrest.MatcherAssert;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.ExactComparisonCriteria;
import org.junit.internal.InexactComparisonCriteria;
@@ -737,7 +736,7 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* values
*
* @see org.hamcrest.CoreMatchers
- * @see org.junit.matchers.JUnitMatchers
+ * @see org.hamcrest.MatcherAssert
*/
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
@@ -749,7 +748,6 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* the reason and information about the matcher and failing value. Example:
*
* <pre>
- * :
* assertThat(&quot;Help! Integers don't work&quot;, 0, is(1)); // fails:
* // failure message:
* // Help! Integers don't work
@@ -775,19 +773,10 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* values
*
* @see org.hamcrest.CoreMatchers
- * @see org.junit.matchers.JUnitMatchers
+ * @see org.hamcrest.MatcherAssert
*/
public static <T> void assertThat(String reason, T actual,
Matcher<? super T> matcher) {
- if (!matcher.matches(actual)) {
- Description description= new StringDescription();
- description.appendText(reason);
- description.appendText("\nExpected: ");
- description.appendDescriptionOf(matcher);
- description.appendText("\n got: ");
- description.appendValue(actual);
- description.appendText("\n");
- throw new java.lang.AssertionError(description.toString());
- }
+ MatcherAssert.assertThat(reason, actual, matcher);
}
}
@@ -434,7 +434,7 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
String expected = "expected";
String actual = "actual";
- String expectedMessage = "identifier\nExpected: \"expected\"\n got: \"actual\"\n";
+ String expectedMessage = "identifier\nExpected: \"expected\"\n but: was \"actual\"";
try {
assertThat("identifier", actual, equalTo(expected));
@@ -444,7 +444,7 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
}
@Test public void assertThatIncludesAdvancedMismatch() {
- String expectedMessage = "identifier\nExpected: is an instance of java.lang.Integer\n got: \"actual\"\n";
+ String expectedMessage = "identifier\nExpected: is an instance of java.lang.Integer\n but: \"actual\" is a java.lang.String";
try {
assertThat("identifier", "actual", is(instanceOf(Integer.class)));
@@ -457,7 +457,7 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
String expected = "expected";
String actual = "actual";
- String expectedMessage = "\nExpected: \"expected\"\n got: \"actual\"\n";
+ String expectedMessage = "\nExpected: \"expected\"\n but: was \"actual\"";
try {
assertThat(actual, equalTo(expected));
@@ -59,12 +59,12 @@
@Test public void usedErrorCollectorCheckThatShouldFail() {
PrintableResult testResult= testResult(UsesErrorCollectorCheckThat.class);
- assertThat(testResult, hasFailureContaining("got: <3>"));
- assertThat(testResult, hasFailureContaining("got: <5>"));
+ assertThat(testResult, hasFailureContaining("was <3>"));
+ assertThat(testResult, hasFailureContaining("was <5>"));
assertThat(testResult, hasFailureContaining("reason 1"));
- assertThat(testResult, hasFailureContaining("got: <7>"));
+ assertThat(testResult, hasFailureContaining("was <7>"));
assertThat(testResult, hasFailureContaining("reason 2"));
- assertThat(testResult, hasFailureContaining("got: <9>"));
+ assertThat(testResult, hasFailureContaining("was <9>"));
}
public static class UsesErrorCollectorCheckSucceeds {

0 comments on commit a0eea13

Please sign in to comment.