Skip to content

Commit

Permalink
Consistent exception testing in AssertionTest (junit-team#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
panchenko authored and kcooney committed May 25, 2017
1 parent 9ee3814 commit 9cdf06c
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -341,7 +347,7 @@ public void multiDimensionalArraysDifferentLengthMessage() {
return;
}

fail("Expected AssertionError to be thrown");
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -353,7 +359,7 @@ public void multiDimensionalArraysDifferentLengthNoMessage() {
return;
}

fail("Expected AssertionError to be thrown");
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -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
Expand Down Expand Up @@ -418,21 +426,23 @@ public void notEqualsObjectWithNullWithMessage() {
Object o = new Object();
try {
assertEquals("message", null, o);
fail();
} catch (AssertionError e) {
assertEquals("message expected:<null> but was:<" + o.toString() + ">", e.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
public void notEqualsNullWithObjectWithMessage() {
Object o = new Object();
try {
assertEquals("message", o, null);
fail();
} catch (AssertionError e) {
assertEquals("message expected:<" + o.toString() + "> but was:<null>", e.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test(expected = AssertionError.class)
Expand Down Expand Up @@ -516,21 +526,23 @@ public void naNsAreEqual() {
public void nullNullmessage() {
try {
assertNull("junit");
fail();
} catch (AssertionError e) {
assertEquals("expected null, but was:<junit>", e.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@SuppressWarnings("unused")
@Test
public void nullWithMessage() {
try {
assertNull("message", "hello");
fail();
} catch (AssertionError exception) {
assertEquals("message expected null, but was:<hello>", exception.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand Down Expand Up @@ -561,43 +573,47 @@ public void objectsAreSame() {
public void sameWithMessage() {
try {
assertSame("not same", "hello", "good-bye");
fail();
} catch (AssertionError exception) {
assertEquals("not same expected same:<hello> was not:<good-bye>",
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:<hello> was not:<good-bye>", exception.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
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
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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -709,7 +737,9 @@ public void nullAndStringNullPrintCorrectError() {
assertEquals(null, "null");
} catch (AssertionError e) {
assertEquals("expected: null<null> but was: java.lang.String<null>", e.getMessage());
return;
}
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test(expected = AssertionError.class)
Expand All @@ -735,7 +765,7 @@ public void nullToString() {
return;
}

fail("AssertionError expected");
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test(expected = AssertionError.class)
Expand Down Expand Up @@ -769,7 +799,7 @@ public void assertNotEqualsIncludesCorrectMessage() {
return;
}

fail("Failed on assertion.");
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -784,7 +814,7 @@ public void assertNotEqualsIncludesTheValueBeingTested() {
return;
}

fail("Failed on assertion.");
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -860,7 +890,7 @@ public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
assertEquals("inner-message", ex.getCause().getMessage());
return;
}
fail();
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -875,7 +905,7 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
assertSame(npe, error.getCause());
return;
}
fail();
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -892,7 +922,7 @@ public void expectThrowsUsesCanonicalNameUponTypeMismatch() {
assertSame(npe, error.getCause());
return;
}
fail();
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -910,7 +940,7 @@ public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() {
assertSame(npe, error.getCause());
return;
}
fail();
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
Expand All @@ -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 {
Expand Down

0 comments on commit 9cdf06c

Please sign in to comment.