-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consistent exception testing in AssertionTest #1455
Conversation
@@ -34,6 +34,10 @@ | |||
// assert false; | |||
// } | |||
|
|||
private void failAssertionErrorExpected() { | |||
throw new AssertionError("AssertionError expected"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's intentional that exception is thrown directly here, as this class also have tests for the fail()
method. So I think not using fail()
in this particular case is a good thing :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer to extract a static constant for "AssertionError expected" and inline this method. That's because it's 100% clear that this code is wrong:
try {
fail("woops');
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
} catch (AssertionError expected) {
...
@@ -34,6 +34,10 @@ | |||
// assert false; | |||
// } | |||
|
|||
private void failAssertionErrorExpected() { | |||
throw new AssertionError("AssertionError expected"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer to extract a static constant for "AssertionError expected" and inline this method. That's because it's 100% clear that this code is wrong:
try {
fail("woops');
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
} catch (AssertionError expected) {
...
@@ -367,9 +375,11 @@ public void arraysWithNullElementEqual() { | |||
public void stringsDifferWithUserMessage() { | |||
try { | |||
assertEquals("not equal", "one", "two"); | |||
} catch (Throwable exception) { | |||
} catch (AssertionError exception) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can change this to catch ComparisonFailure
instead (in which case, it's perfectly fine to call fail()
in the try
block)
Thanks! |
That was discovered while discussing #1454, so I would like it be more consistent.