-
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
fix assertEquals() if toString() returns null #1454
Conversation
return; | ||
} | ||
|
||
fail("Failed on assertion."); |
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.
Why do we need this fail
statement?
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.
Only in case assertEquals()
does nothing.
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.
Well, the same holds true for all the other tests in this class, right? If so, please remove the return
and the fail()
statements.
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.
@marcphilipp there are different kind of tests in this class:
- some have
@Test(expected = AssertionError.class)
- some have
return
+fail()
- some have only
fail()
insidetry-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.
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.
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.
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 would agree with "a bit odd", however it's shorter. Will revert back to return
:-)
return; | ||
} | ||
|
||
fail("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.
@kcooney I would like this extracted as a function, and will update other tests then separately.
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.
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.
Fixes #1453