Skip to content

Commit

Permalink
Hamcrest 1.2 documented in release notes
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Beck <kent@threeriversinstitute.org>
  • Loading branch information
David Saff authored and KentBeck committed May 29, 2009
1 parent a5aa7cf commit 79b7c3e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
31 changes: 31 additions & 0 deletions doc/ReleaseNotes4.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@
### Timeouts ###
- Tests that time out now show the stack trace of the test thread.

### Matchers ###
- Hamcrest 1.2 is now incorporated.
- The following methods from `JUnitMatchers` are deprecated, and moved to `CoreMatchers`:
- `JUnitMatchers.hasItem` is now `CoreMatchers.hasItem`
- `JUnitMatchers.hasItems` is now `CoreMatchers.hasItems`
- `JUnitMatchers.containsString` is now `CoreMatchers.containsString`

- Matchers now have more informative mismatch descriptions. For example:

@SuppressWarnings("unchecked")
@Test public void stringIsAnInteger() {
assertThat("identifier", "actual", matches(is(Integer.class)));
// prints:
// Expected: is an instance of java.lang.Integer
// but: \"actual\" is a java.lang.String
}

- Some matchers have slightly changed type signatures, especially those created
by `is()` and `equalTo`. Everything should work, except see `BothTest` for an
example of how the `both().and()` and `either().or()` constructs may be
affected

Expected: (is an instance of java.lang.Integer)\n but: \"actual\" is a java.lang.String
@SuppressWarnings("unchecked")
public void testMismatchDescriptionDescribesFirstFailingMatch() {
assertThat("bad speling",
allOf(containsString("bad"), containsString("spelling")));
// prints: Expected: (contains string "bad" and contains string "spelling")
but: contains string "spelling" was "bad speling"
}

### Docs ###
- Javadocs now link to online JDK javadocs (bug 2090230)
- Parameterized runner javadocs improved (bug 2186792)
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,12 +788,12 @@ public static <T> void assertThat(T actual, Matcher<T> matcher) {
public static <T> void assertThat(String reason, T actual,
Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description= new StringDescription();
description.appendText(reason);
description.appendText("\nExpected: ");
matcher.describeTo(description);
description.appendText("\n got: ").appendValue(actual)
.appendText("\n");
Description description = new StringDescription();
description.appendText(reason)
.appendText("\nExpected: ")
.appendDescriptionOf(matcher)
.appendText("\n but: ");
matcher.describeMismatch(actual, description);
throw new java.lang.AssertionError(description.toString());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/junit/matchers/JUnitMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMat
* @deprecated use CoreMatchers.everyItem directly
*/
@Deprecated
public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {
public static <T> Matcher<Iterable<T>> each(final Matcher<T> elementMatcher) {
return CoreMatchers.everyItem(elementMatcher);
}

Expand Down
18 changes: 16 additions & 2 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.junit.tests.assertion;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.matches;

import java.math.BigDecimal;

Expand Down Expand Up @@ -415,20 +418,31 @@ 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));
} catch (AssertionError e) {
assertEquals(expectedMessage, e.getMessage());
}
}

@SuppressWarnings("unchecked")
@Test public void assertThatIncludesAdvancedMismatch() {
String expectedMessage = "identifier\nExpected: is an instance of java.lang.Integer\n but: \"actual\" is a java.lang.String";

try {
assertThat("identifier", "actual", matches(is(Integer.class)));
} catch (AssertionError e) {
assertEquals(expectedMessage, e.getMessage());
}
}

@Test public void assertThatDescriptionCanBeElided() {
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));
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/junit/tests/assertion/BothTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.junit.tests.assertion;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
Expand Down

0 comments on commit 79b7c3e

Please sign in to comment.