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

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

Merged
merged 5 commits into from
May 19, 2017

Conversation

panchenko
Copy link
Contributor

Fixes #1453

return;
}

fail("Failed on assertion.");
Copy link
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
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
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
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
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
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
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
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