Permalink
Browse files

* Removed empty line in ExpectedException

* Moved CauseMatcher to ResultMatchers and splitted it up in two
matchers (failureIs and causedBy)
* Added tests for both matchers
  • Loading branch information...
1 parent 5392910 commit 052fc095d5b4a8af643d0e4402a504716ad8eb03 @arjenw committed Feb 2, 2012
@@ -4,6 +4,8 @@
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
/**
* Matchers on a PrintableResult, to enable JUnit self-tests.
@@ -67,4 +69,40 @@ public void describeTo(Description description) {
}
};
}
+
+ /**
+ * Matches if the failure matches the given {@code failureMatcher}
+ */
+ public static Matcher<Result> failureIs(final Matcher<? super Throwable> failureMatcher) {
+ return new TypeSafeMatcher<Result>() {
+ public void describeTo(Description description) {
+ description.appendText("failure is ");
+ failureMatcher.describeTo(description);
+ }
+
+ @Override
+ public boolean matchesSafely(Result item) {
+ for(Failure f: item.getFailures())
+ return failureMatcher.matches(f.getException());
+ return false;
+ }
+ };
+ }
+
+ /**
+ * Matches if the cause of the exception matches the given {@code causeMatcher}
+ */
+ public static Matcher<Throwable> causedBy(final Matcher<? super Throwable> causeMatcher) {
+ return new TypeSafeMatcher<Throwable>() {
+ public void describeTo(Description description) {
+ description.appendText("caused by ");
+ causeMatcher.describeTo(description);
+ }
+
+ @Override
+ public boolean matchesSafely(Throwable item) {
+ return causeMatcher.matches(item.getCause());
+ }
+ };
+ }
}
@@ -111,7 +111,6 @@ public void evaluate() throws Throwable {
} catch (Throwable e) {
if (fMatcher == null)
throw e;
-
Assert.assertThat(e, fMatcher);
return;
}
@@ -1,11 +1,17 @@
package org.junit.tests.experimental.results;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.experimental.results.ResultMatchers.causedBy;
+import static org.junit.experimental.results.ResultMatchers.failureIs;
import static org.junit.internal.matchers.StringContains.containsString;
+import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.experimental.results.ResultMatchers;
import org.junit.experimental.theories.Theory;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
public class ResultMatchersTest {
@Test
public void hasFailuresHasGoodDescription() {
@@ -18,4 +24,30 @@ public void hasFailuresDescriptionReflectsInput(int i) {
assertThat(ResultMatchers.failureCountIs(i).toString(),
containsString("" + i));
}
+
+ private Result createResult() throws Exception {
+ Result result= new Result();
+ result.createListener().testFailure(new Failure(null, new IllegalArgumentException()));
+ return result;
+ }
+
+ @Test
+ public void failureIsCorrect() throws Exception {
+ assertThat(createResult(), failureIs(instanceOf(IllegalArgumentException.class)));
+ }
+
+ @Test(expected=AssertionError.class)
+ public void failureIsFailing() throws Exception {
+ assertThat(createResult(), failureIs(instanceOf(IllegalStateException.class)));
+ }
+
+ @Test
+ public void causedByCorrect() {
+ assertThat(new IllegalStateException(new IllegalArgumentException()), causedBy(instanceOf(IllegalArgumentException.class)));
+ }
+
+ @Test(expected=AssertionError.class)
+ public void causedByFailing() {
+ assertThat(new IllegalStateException(new IllegalArgumentException()), causedBy(CoreMatchers.instanceOf(IllegalStateException.class)));
+ }
}
@@ -1,21 +1,21 @@
package org.junit.tests.experimental.rules;
import static org.hamcrest.CoreMatchers.any;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
+import static org.junit.experimental.results.ResultMatchers.causedBy;
+import static org.junit.experimental.results.ResultMatchers.failureIs;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import static org.junit.matchers.JUnitMatchers.both;
-import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.rules.ExpectedException;
import org.junit.runner.JUnitCore;
-import org.junit.runner.Result;
-import org.junit.runner.notification.Failure;
public class ExpectedExceptionRuleTest {
public static class HasExpectedException {
@@ -64,29 +64,10 @@ public void unExpectedExceptionFails() {
hasSingleFailureContaining("Expected: an instance of java.lang.NullPointerException"));
}
- public static class CauseMatcher extends TypeSafeMatcher<Result> {
- private final Class<? extends Throwable> causeClass;
-
- public CauseMatcher(Class<? extends Throwable> causeClass) {
- this.causeClass= causeClass;
- }
-
- public void describeTo(Description description) {
- description.appendText("cause being an instanceof ").appendText(causeClass.getName());
- }
-
- @Override
- public boolean matchesSafely(Result item) {
- for(Failure f: item.getFailures())
- return CoreMatchers.instanceOf(causeClass).matches(f.getException().getCause());
- return false;
- }
- }
-
@Test
public void expectedExceptionClauseExists() {
assertThat(new JUnitCore().run(HasWrongExpectedException.class),
- new CauseMatcher(IllegalArgumentException.class));
+ failureIs(causedBy(instanceOf(IllegalArgumentException.class))));
}
public static class HasWrongMessage {

0 comments on commit 052fc09

Please sign in to comment.