Skip to content

Commit

Permalink
Rename withStacktrace() to isException()/isThrowable()
Browse files Browse the repository at this point in the history
Following suggestion by @sf105
  • Loading branch information
marcphilipp committed Jul 20, 2012
1 parent 872faea commit a37fd9a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
15 changes: 12 additions & 3 deletions src/main/java/org/junit/matchers/JUnitMatchers.java
Expand Up @@ -109,9 +109,18 @@ public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher)
/**
* @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);
}
}
4 changes: 2 additions & 2 deletions 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;
Expand Down Expand Up @@ -59,7 +59,7 @@ private Matcher<Throwable> getCompositeMatcher() {
}

private Matcher<Throwable> createCompositeMatcher() {
return withStacktrace(allOfTheMatchers());
return isThrowable(allOfTheMatchers());
}

private Matcher<Throwable> allOfTheMatchers() {
Expand Down
Expand Up @@ -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
Expand All @@ -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"));
}
Expand Down

0 comments on commit a37fd9a

Please sign in to comment.