From 6384edfffe7f8a2850d2bcde9a16a8e780734ebe Mon Sep 17 00:00:00 2001 From: Urs Metz Date: Sun, 23 Dec 2012 15:12:09 +0100 Subject: [PATCH 1/3] fail when AssertionError expected but not thrown When a user expected the production code to throw an AssertionError by using ExpectedException (together with the #handleAssertionErrors option) but the code under test didn't do so the test was not failing. This was because the ExpectedException rule caught its own AssertionError issued to signal that the expected exception was not thrown. --- .../java/org/junit/rules/ExpectedException.java | 17 ++++++++++------- .../rules/ExpectedExceptionTest.java | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/junit/rules/ExpectedException.java b/src/main/java/org/junit/rules/ExpectedException.java index 32c2874d152a..e559bc544c6c 100644 --- a/src/main/java/org/junit/rules/ExpectedException.java +++ b/src/main/java/org/junit/rules/ExpectedException.java @@ -179,23 +179,22 @@ public ExpectedExceptionStatement(Statement base) { public void evaluate() throws Throwable { try { fNext.evaluate(); - if (fMatcherBuilder.expectsThrowable()) { - failDueToMissingException(); - } } catch (AssumptionViolatedException e) { optionallyHandleException(e, handleAssumptionViolatedExceptions); + return; } catch (AssertionError e) { optionallyHandleException(e, handleAssertionErrors); + return; } catch (Throwable e) { handleException(e); + return; + } + if (fMatcherBuilder.expectsThrowable()) { + failDueToMissingException(); } } } - private void failDueToMissingException() throws AssertionError { - fail(missingExceptionMessage()); - } - private void optionallyHandleException(Throwable e, boolean handleException) throws Throwable { if (handleException) { @@ -212,6 +211,10 @@ private void handleException(Throwable e) throws Throwable { throw e; } } + + private void failDueToMissingException() throws AssertionError { + fail(missingExceptionMessage()); + } private String missingExceptionMessage() { if (isMissingExceptionMessageEmpty()) { diff --git a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java index f0d30a9a5dee..10cc279b7c3b 100644 --- a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java +++ b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java @@ -3,6 +3,7 @@ import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.any; import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.startsWith; @@ -71,6 +72,8 @@ public static Collection testsWithEventMatcher() { {ViolateAssumptionAndExpectException.class, hasSingleAssumptionFailure()}, {ThrowExpectedAssertionError.class, everyTestRunSuccessful()}, + {DontThrowAssertionErrorButExpectOne.class, hasSingleFailureWithMessage( + endsWith("Expected test to throw an instance of java.lang.AssertionError"))}, { ThrowUnexpectedAssertionError.class, hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))}, @@ -288,6 +291,16 @@ public void wrongException() { throw new AssertionError("the expected assertion error"); } } + public static class DontThrowAssertionErrorButExpectOne { + @Rule + public ExpectedException thrown = none(); + + @Test + public void assertionErrorExpectedButNonIsThrown() { + thrown.handleAssertionErrors(); + thrown.expect(AssertionError.class); + } + } public static class ViolateAssumptionAndExpectException { @Rule From aace70a912417166d5646c284d0bde5b81f504fb Mon Sep 17 00:00:00 2001 From: Urs Metz Date: Sun, 23 Dec 2012 21:10:00 +0100 Subject: [PATCH 2/3] assertion on complete error message --- .../tests/experimental/rules/ExpectedExceptionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java index 10cc279b7c3b..53931bc3b4e6 100644 --- a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java +++ b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java @@ -3,7 +3,6 @@ import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.any; import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.startsWith; @@ -72,8 +71,9 @@ public static Collection testsWithEventMatcher() { {ViolateAssumptionAndExpectException.class, hasSingleAssumptionFailure()}, {ThrowExpectedAssertionError.class, everyTestRunSuccessful()}, - {DontThrowAssertionErrorButExpectOne.class, hasSingleFailureWithMessage( - endsWith("Expected test to throw an instance of java.lang.AssertionError"))}, + { + DontThrowAssertionErrorButExpectOne.class, + hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")}, { ThrowUnexpectedAssertionError.class, hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))}, From 04218bee53cd0acf0b4ffe14d9052349900020db Mon Sep 17 00:00:00 2001 From: Urs Metz Date: Fri, 28 Dec 2012 18:40:26 +0100 Subject: [PATCH 3/3] correct formatting --- .../tests/experimental/rules/ExpectedExceptionTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java index 53931bc3b4e6..24eecfee20be 100644 --- a/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java +++ b/src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java @@ -72,8 +72,8 @@ public static Collection testsWithEventMatcher() { hasSingleAssumptionFailure()}, {ThrowExpectedAssertionError.class, everyTestRunSuccessful()}, { - DontThrowAssertionErrorButExpectOne.class, - hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")}, + DontThrowAssertionErrorButExpectOne.class, + hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.AssertionError")}, { ThrowUnexpectedAssertionError.class, hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException"))}, @@ -291,6 +291,7 @@ public void wrongException() { throw new AssertionError("the expected assertion error"); } } + public static class DontThrowAssertionErrorButExpectOne { @Rule public ExpectedException thrown = none();