Skip to content
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

Merged
merged 4 commits into from
May 25, 2017

Conversation

panchenko
Copy link
Contributor

That was discovered while discussing #1454, so I would like it be more consistent.

@@ -34,6 +34,10 @@
// assert false;
// }

private void failAssertionErrorExpected() {
throw new AssertionError("AssertionError expected");
Copy link
Contributor Author

@panchenko panchenko May 19, 2017

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 :-)

Copy link
Member

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");
Copy link
Member

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) {
Copy link
Member

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)

@kcooney kcooney merged commit 9cdf06c into junit-team:master May 25, 2017
@kcooney
Copy link
Member

kcooney commented May 25, 2017

Thanks!

@panchenko panchenko deleted the assertionErrorExpected branch May 26, 2017 00:52
sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants