Skip to content

fix assertEquals() if toString() returns null#1454

Merged
kcooney merged 5 commits intojunit-team:masterfrom
panchenko:issue_1453
May 19, 2017
Merged

fix assertEquals() if toString() returns null#1454
kcooney merged 5 commits intojunit-team:masterfrom
panchenko:issue_1453

Conversation

@panchenko
Copy link
Copy Markdown
Contributor

Fixes #1453

return;
}

fail("Failed on assertion.");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this fail statement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in case assertEquals() does nothing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the same holds true for all the other tests in this class, right? If so, please remove the return and the fail() statements.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcphilipp there are different kind of tests in this class:

  • some have @Test(expected = AssertionError.class)
  • some have return + fail()
  • some have only fail() inside try-catch
  • some don't have fail() / return

As this test is expected to throw an exception and that exception is being checked, I think it's better to make sure an exception was thrown. I agree this variant is too verbose, the best approach would be throwing some new runtime exception from the try-catch block.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling fail from inside of a try block that has catch (AssertionError doesn't do what many people think it does, so I'm strongly against that option. Maybe I'll find time to fix the tests that do that.

I also dislike using @Test(expected = Something.class) for any test method where there is more than one call in the body, since it isn't clear which method should throw the exception. Because using expected should usually only be used for single-method tests, I tend to completely avoid it in my personal projects, but it's usage is very common in the JUnit tests.

Throwing a custom exception inside the try block for something that should be an assertion error is a bit odd, so I'm not thrilled with that option.

I personally strongly in favor of the return and fail idiom for any tests that verify that a test method throws AssertionError unless the test can be written with one line of code (in which case @Test(expected = AssertionError.class) is fine).

Luckily, with Java 8 we can all use lambdas so this will all be moot some day.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with "a bit odd", however it's shorter. Will revert back to return :-)

@kcooney kcooney merged commit a81c5e2 into junit-team:master May 19, 2017
return;
}

fail("AssertionError expected");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kcooney I would like this extracted as a function, and will update other tests then separately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could could do that without hurting readability that is fine. For tests I am fine with some duplication if it makes the code clear.

@panchenko panchenko deleted the issue_1453 branch May 19, 2017 01:46
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.

3 participants