Permalink
Browse files

Don't handle AssertionError.

An AssertionError thrown by the Statement is rethrown. Therefore tests
fail because of the AssertionError and not because of a wrong expected
exception.

Fix #121. Thanks to petur for discovering this bug.
  • Loading branch information...
1 parent 6263b97 commit 6e6bac0b6edfc22483798f82dbf44cf1103d12dc @stefanbirkner committed Sep 11, 2011
View
3 acknowledgements.txt
@@ -129,3 +129,6 @@
Robert Munteanu, Stefan Birkner:
TestWatcher and TestWatchman don't call failed when assumption is violated (github#296).
digulla@github, Stefan Birkner: Removed useless code (github#289).
+
+2011 Sep 10
+ petur@github, Stefan Birkner: Expected exceptions consider assumes and asserts (github#121).
View
2 src/main/java/org/junit/internal/runners/statements/ExpectException.java
@@ -21,6 +21,8 @@ public void evaluate() throws Exception {
try {
fNext.evaluate();
complete = true;
+ } catch (AssertionError e) {
+ throw e;
} catch (AssumptionViolatedException e) {
throw e;
} catch (Throwable e) {
View
2 src/main/java/org/junit/rules/ExpectedException.java
@@ -109,6 +109,8 @@ public ExpectedExceptionStatement(Statement base) {
public void evaluate() throws Throwable {
try {
fNext.evaluate();
+ } catch (AssertionError e) {
+ throw e;
} catch (AssumptionViolatedException e) {
throw e;
} catch (Throwable e) {
View
27 src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionRuleTest.java
@@ -1,9 +1,12 @@
package org.junit.tests.experimental.rules;
+import static junit.framework.Assert.fail;
import static org.hamcrest.CoreMatchers.any;
+import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
+import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import static org.junit.matchers.JUnitMatchers.both;
@@ -237,7 +240,7 @@ public void failsWithMultipleMatchers() {
@Test
public void violatedAssumption() {
- // expect an exception, which is not a AssumptionVioltatedException
+ // expect an exception, which is not an AssumptionVioltatedException
thrown.expect(IllegalArgumentException.class);
assumeTrue(false);
}
@@ -248,4 +251,26 @@ public void dontFailForViolatedAssumption() {
assertThat(testResult(DontFailForViolatedAssumption.class),
isSuccessful());
}
+
+ public static class FailForAssertion {
+ @Rule
+ public ExpectedException thrown= ExpectedException.none();
+
+ @Test
+ public void failingTest() {
+ // expect an exception, which is not an AssertionError
+ thrown.expect(NullPointerException.class);
+ fail("Failed because of assertion.");
+ throw new NullPointerException();
+ }
+ }
+
+ @Test
+ public void handOverAssertionError() {
+ assertThat(
+ testResult(FailForAssertion.class),
+ both(
+ not(hasFailureContaining("java.lang.NullPointerException")))
+ .and(hasFailureContaining("Failed because of assertion.")));
+ }
}
View
36 src/test/java/org/junit/tests/running/classes/BlockJUnit4ClassRunnerTest.java
@@ -0,0 +1,36 @@
+package org.junit.tests.running.classes;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.fail;
+import org.junit.Test;
+import org.junit.runner.Result;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.InitializationError;
+
+public class BlockJUnit4ClassRunnerTest {
+
+ public static class FailForAssertion {
+ @Test(expected= NullPointerException.class)
+ public void failingTest() {
+ fail("Failed because of assertion.");
+ throw new NullPointerException();
+ }
+ }
+
+ @Test
+ public void handOverAssertionError() throws Exception {
+ Result result= testResult(FailForAssertion.class);
+ assertEquals("Failed because of assertion.", result.getFailures()
+ .get(0).getMessage());
+ }
+
+ private Result testResult(Class<?> test) throws InitializationError {
+ Result result= new Result();
+ RunNotifier notifier= new RunNotifier();
+ notifier.addListener(result.createListener());
+ BlockJUnit4ClassRunner runner= new BlockJUnit4ClassRunner(test);
+ runner.run(notifier);
+ return result;
+ }
+}

0 comments on commit 6e6bac0

Please sign in to comment.