Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rename withStacktrace() to isException()/isThrowable()

Following suggestion by @sf105
  • Loading branch information...
commit a37fd9ac8b2afc54384fd32f470b54aa69dc620a 1 parent 872faea
@marcphilipp marcphilipp authored
View
12 src/main/java/org/junit/internal/matchers/StacktracePrintingMatcher.java
@@ -9,14 +9,14 @@
/**
* A matcher that delegates to throwableMatcher and in addition appends the
- * stacktrace of the actual item in case of a mismatch.
+ * stacktrace of the actual Throwable in case of a mismatch.
*/
public class StacktracePrintingMatcher<T extends Throwable> extends
org.hamcrest.TypeSafeMatcher<T> {
private final Matcher<T> fThrowableMatcher;
- private StacktracePrintingMatcher(Matcher<T> throwableMatcher) {
+ public StacktracePrintingMatcher(Matcher<T> throwableMatcher) {
fThrowableMatcher= throwableMatcher;
}
@@ -43,8 +43,14 @@ private String readStacktrace(Throwable throwable) {
}
@Factory
- public static <T extends Throwable> Matcher<T> withStacktrace(
+ public static <T extends Throwable> Matcher<T> isThrowable(
Matcher<T> throwableMatcher) {
return new StacktracePrintingMatcher<T>(throwableMatcher);
}
+
+ @Factory
+ public static <T extends Exception> Matcher<T> isException(
+ Matcher<T> exceptionMatcher) {
+ return new StacktracePrintingMatcher<T>(exceptionMatcher);
+ }
}
View
15 src/main/java/org/junit/matchers/JUnitMatchers.java
@@ -109,9 +109,18 @@
/**
* @param throwableMatcher
* @return A matcher that delegates to throwableMatcher and in addition
- * appends the stacktrace of the actual item in case of a mismatch.
+ * appends the stacktrace of the actual Throwable in case of a mismatch.
*/
- public static <T extends Throwable> Matcher<T> withStacktrace(Matcher<T> throwableMatcher) {
- return StacktracePrintingMatcher.withStacktrace(throwableMatcher);
+ public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) {
+ return StacktracePrintingMatcher.isThrowable(throwableMatcher);
+ }
+
+ /**
+ * @param exceptionMatcher
+ * @return A matcher that delegates to exceptionMatcher and in addition
+ * appends the stacktrace of the actual Exception in case of a mismatch.
+ */
+ public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) {
+ return StacktracePrintingMatcher.isException(exceptionMatcher);
}
}
View
4 src/main/java/org/junit/rules/ExpectedExceptionMatcher.java
@@ -1,7 +1,7 @@
package org.junit.rules;
import static org.hamcrest.CoreMatchers.allOf;
-import static org.junit.matchers.JUnitMatchers.withStacktrace;
+import static org.junit.matchers.JUnitMatchers.isThrowable;
import java.util.ArrayList;
import java.util.LinkedList;
@@ -59,7 +59,7 @@ public void describeTo(Description description) {
}
private Matcher<Throwable> createCompositeMatcher() {
- return withStacktrace(allOfTheMatchers());
+ return isThrowable(allOfTheMatchers());
}
private Matcher<Throwable> allOfTheMatchers() {
View
11 src/test/java/org/junit/internal/matchers/StacktracePrintingMatcherTest.java
@@ -2,24 +2,25 @@
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.junit.internal.matchers.StacktracePrintingMatcher.withStacktrace;
+import static org.junit.internal.matchers.StacktracePrintingMatcher.isException;
+import static org.junit.internal.matchers.StacktracePrintingMatcher.isThrowable;
import org.junit.Test;
public class StacktracePrintingMatcherTest {
@Test
public void succeedsWhenInnerMatcherSuceeds() throws Exception {
- assertTrue(withStacktrace(is(any(Throwable.class))).matches(new Exception()));
+ assertTrue(isThrowable(any(Throwable.class)).matches(new Exception()));
}
@Test
public void failsWhenInnerMatcherFails() throws Exception {
- assertFalse(withStacktrace(is(notNullValue(Exception.class))).matches(null));
+ assertFalse(isException(notNullValue(Exception.class)).matches(null));
}
@Test
@@ -28,7 +29,7 @@ public void assertThatIncludesStacktrace() {
Exception expected= new NullPointerException();
try {
- assertThat(actual, withStacktrace(is(expected)));
+ assertThat(actual, isThrowable(equalTo(expected)));
} catch (AssertionError e) {
assertThat(e.getMessage(), containsString("Stacktrace was: java.lang.IllegalArgumentException: my message"));
}
Please sign in to comment.
Something went wrong with that request. Please try again.