diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index 0242bb50d017..ed7ddd1d0980 100755 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -34,6 +34,8 @@ public class AssertionTest { // assert false; // } + private static final String ASSERTION_ERROR_EXPECTED = "AssertionError expected"; + @Test(expected = AssertionError.class) public void fails() { Assert.fail(); @@ -45,7 +47,9 @@ public void failWithNoMessageToString() { Assert.fail(); } catch (AssertionError exception) { assertEquals("java.lang.AssertionError", exception.toString()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -54,7 +58,9 @@ public void failWithMessageToString() { Assert.fail("woops!"); } catch (AssertionError exception) { assertEquals("java.lang.AssertionError: woops!", exception.toString()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -82,7 +88,7 @@ public void arraysExpectedNullMessage() { assertEquals("not equal: expected array was null", exception.getMessage()); return; } - fail("should have thrown an exception"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -93,7 +99,7 @@ public void arraysActualNullMessage() { assertEquals("not equal: actual array was null", exception.getMessage()); return; } - fail("should have thrown an exception"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -319,7 +325,7 @@ private void assertArrayEqualsFailure(Object[] expecteds, Object[] actuals, Stri assertEquals(expectedMessage, e.getMessage()); return; } - fail("should have thrown an exception"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } private void assertArrayEqualsFailure(String message, Object[] expecteds, Object[] actuals, String expectedMessage) { @@ -329,7 +335,7 @@ private void assertArrayEqualsFailure(String message, Object[] expecteds, Object assertEquals(expectedMessage, e.getMessage()); return; } - fail("should have thrown an exception"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -341,7 +347,7 @@ public void multiDimensionalArraysDifferentLengthMessage() { return; } - fail("Expected AssertionError to be thrown"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -353,7 +359,7 @@ public void multiDimensionalArraysDifferentLengthNoMessage() { return; } - fail("Expected AssertionError to be thrown"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -367,9 +373,11 @@ public void arraysWithNullElementEqual() { public void stringsDifferWithUserMessage() { try { assertEquals("not equal", "one", "two"); - } catch (Throwable exception) { + } catch (ComparisonFailure exception) { assertEquals("not equal expected:<[one]> but was:<[two]>", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -418,10 +426,11 @@ public void notEqualsObjectWithNullWithMessage() { Object o = new Object(); try { assertEquals("message", null, o); - fail(); } catch (AssertionError e) { assertEquals("message expected: but was:<" + o.toString() + ">", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -429,10 +438,11 @@ public void notEqualsNullWithObjectWithMessage() { Object o = new Object(); try { assertEquals("message", o, null); - fail(); } catch (AssertionError e) { assertEquals("message expected:<" + o.toString() + "> but was:", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test(expected = AssertionError.class) @@ -516,10 +526,11 @@ public void naNsAreEqual() { public void nullNullmessage() { try { assertNull("junit"); - fail(); } catch (AssertionError e) { assertEquals("expected null, but was:", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @SuppressWarnings("unused") @@ -527,10 +538,11 @@ public void nullNullmessage() { public void nullWithMessage() { try { assertNull("message", "hello"); - fail(); } catch (AssertionError exception) { assertEquals("message expected null, but was:", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -561,21 +573,23 @@ public void objectsAreSame() { public void sameWithMessage() { try { assertSame("not same", "hello", "good-bye"); - fail(); } catch (AssertionError exception) { assertEquals("not same expected same: was not:", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test public void sameNullMessage() { try { assertSame("hello", "good-bye"); - fail(); } catch (AssertionError exception) { assertEquals("expected same: was not:", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -583,10 +597,11 @@ public void notSameWithMessage() { Object o = new Object(); try { assertNotSame("message", o, o); - fail(); } catch (AssertionError exception) { assertEquals("message expected not same", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -594,10 +609,11 @@ public void notSameNullMessage() { Object o = new Object(); try { assertNotSame(o, o); - fail(); } catch (AssertionError exception) { assertEquals("expected not same", exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -607,27 +623,31 @@ public void nullMessage() { } catch (AssertionError exception) { // we used to expect getMessage() to return ""; see failWithNoMessageToString() assertNull(exception.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test public void nullMessageDisappearsWithStringAssertEquals() { try { assertEquals(null, "a", "b"); - fail(); } catch (ComparisonFailure e) { assertEquals("expected:<[a]> but was:<[b]>", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test public void nullMessageDisappearsWithAssertEquals() { try { assertEquals(null, 1, 2); - fail(); } catch (AssertionError e) { assertEquals("expected:<1> but was:<2>", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test(expected = AssertionError.class) @@ -661,7 +681,9 @@ public void errorMessageDistinguishesDifferentValuesWithSameToString() { assertEquals("4", new Integer(4)); } catch (AssertionError e) { assertEquals("expected: java.lang.String<4> but was: java.lang.Integer<4>", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -675,7 +697,9 @@ public void assertThatIncludesDescriptionOfTestedValueInErrorMessage() { assertThat("identifier", actual, equalTo(expected)); } catch (AssertionError e) { assertEquals(expectedMessage, e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -686,7 +710,9 @@ public void assertThatIncludesAdvancedMismatch() { assertThat("identifier", "actual", is(instanceOf(Integer.class))); } catch (AssertionError e) { assertEquals(expectedMessage, e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -700,7 +726,9 @@ public void assertThatDescriptionCanBeElided() { assertThat(actual, equalTo(expected)); } catch (AssertionError e) { assertEquals(expectedMessage, e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -709,7 +737,9 @@ public void nullAndStringNullPrintCorrectError() { assertEquals(null, "null"); } catch (AssertionError e) { assertEquals("expected: null but was: java.lang.String", e.getMessage()); + return; } + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test(expected = AssertionError.class) @@ -735,7 +765,7 @@ public void nullToString() { return; } - fail("AssertionError expected"); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test(expected = AssertionError.class) @@ -769,7 +799,7 @@ public void assertNotEqualsIncludesCorrectMessage() { return; } - fail("Failed on assertion."); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -784,7 +814,7 @@ public void assertNotEqualsIncludesTheValueBeingTested() { return; } - fail("Failed on assertion."); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -830,7 +860,7 @@ public void expectThrowsIncludesAnInformativeDefaultMessage() { assertEquals("expected java.lang.Throwable to be thrown, but nothing was thrown", ex.getMessage()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -860,7 +890,7 @@ public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() { assertEquals("inner-message", ex.getCause().getMessage()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -875,7 +905,7 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() { assertSame(npe, error.getCause()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -892,7 +922,7 @@ public void expectThrowsUsesCanonicalNameUponTypeMismatch() { assertSame(npe, error.getCause()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -910,7 +940,7 @@ public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() { assertSame(npe, error.getCause()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } @Test @@ -923,7 +953,7 @@ public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() { + " but nothing was thrown", error.getMessage()); return; } - fail(); + throw new AssertionError(ASSERTION_ERROR_EXPECTED); } private static class NestedException extends RuntimeException {