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

Delete assertThrows() #1396

Closed
wants to merge 3 commits into from
Closed

Conversation

kcooney
Copy link
Member

@kcooney kcooney commented Nov 30, 2016

Delete expectThrows(). Change assertThrows() to return the caught exception.

JUnit 5 decided to make the same change in the Assertions class, so this
change makes the JUnit4 API consistent.

@kcooney kcooney added the 4.13 label Nov 30, 2016
@stefanbirkner
Copy link
Contributor

The Javadoc of ThrowingRunnable has a reference to expectThrows. Could you change that, please.

…eption.

JUnit 5 decided to make the same change in the Assertions class, so this
change makes the JUnit4 API consistent.
@kcooney kcooney force-pushed the remove-expectThrows branch from 2fd6312 to 02a7800 Compare December 2, 2016 17:08
@kcooney
Copy link
Member Author

kcooney commented Dec 2, 2016

Fixed comment that referenced expectThrows, rebased and squashed commits

@kcooney
Copy link
Member Author

kcooney commented Dec 2, 2016

Thanks, Marc. I am going to wait for one or two more approvals.

@kcooney kcooney mentioned this pull request Dec 2, 2016
@kcooney
Copy link
Member Author

kcooney commented Dec 2, 2016

I also requested feedback on the original pull request.

@rschmitt
Copy link
Contributor

rschmitt commented Dec 4, 2016

Is there a single precedent anywhere in JUnit for an assert function that does not return void? Was a motivation for this change even spelled out somewhere? In terms of naming conventions, this strikes me as the equivalent of creating a function called toString that writes something to standard out and returns void instead of String.

@kcooney
Copy link
Member Author

kcooney commented Dec 5, 2016

@rschmitt see junit-team/junit5#531 for the discussion about the associated JUnit 5 APIs.

@kcooney
Copy link
Member Author

kcooney commented Dec 11, 2016

@rschmitt did you have a chance to read that thread?

Personally I don't have a strong opinion about how these methods should be named (the name expectThrows() never bothered me). I just want the APIs for making assertions about exceptions to be consistent between JUnit 4 and JUnit 5.

@rschmitt
Copy link
Contributor

The only new point I encountered in the other thread is that "expect" terminology has different connotations in some other frameworks. The only specific example of that was Google Test, which is actually a C++ testing framework. Since we're drawing analogies to non-Java languages, I'll point out the expect method from Rust's Option type, which corresponds to expectThrows semantics perfectly (return the expected thing if it's there, panic if it's not):

https://doc.rust-lang.org/std/option/enum.Option.html#method.expect

There is another argument I saw which I would like to directly refute, this one from @sbrannen:

Aside from the two fail methods in Assertions, expectThrows is the only method not named assert*.

To this I respond: so what? The key point here is that expectThrows is the only method in Assertions that returns anything, and yet nobody objects to that. (I don't recall any proposals to remove expectThrows on the grounds of uselessness.) Renaming expectThrows to assertThrows despite this essential difference is fake simplicity.

@kcooney kcooney force-pushed the remove-expectThrows branch from cc11427 to 056af41 Compare December 14, 2016 15:43
@kcooney kcooney force-pushed the remove-expectThrows branch from 056af41 to 2fb2db1 Compare December 14, 2016 15:44
@kcooney kcooney changed the title Delete expectThrows() Delete assertThrows() Dec 14, 2016
@kcooney
Copy link
Member Author

kcooney commented Dec 14, 2016

Regardless of the name in JUnit5, I'm coming around to the decision that expectThrows is a better name for JUnit4 (see the discussion on the JUnit5 thread)

Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

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

Regardless of the name in JUnit5, I'm coming around to the decision that expectThrows is a better name for JUnit4 (see the discussion on the JUnit5 thread)

I strongly disagree. I have done quite a few talks in the last couple of months and often got the feedback that expectThrows should be called assertThrows.

In terms of naming conventions, this strikes me as the equivalent of creating a function called toString that writes something to standard out and returns void instead of String.

I see that differently. The name assertThrows is perfectly fine because it does assert that an exception is thrown. Therefore, it does match the other methods in Assert, it just returns the actual exception as additional info.

@kcooney
Copy link
Member Author

kcooney commented Dec 14, 2016

The method executes a lambda, catches the thrown exception, asserts that the exception's type matches the passes-in class, then returns the caught exception; the assertion is only one small part So it is quite different than all the other methods.

@panchenko
Copy link
Contributor

There is no harm in having both like in TestNG, for ~1.5 years already.

@kcooney
Copy link
Member Author

kcooney commented Dec 14, 2016

@panchenko what specifically in TestNG are you referring to?

@rschmitt
Copy link
Contributor

rschmitt commented Dec 14, 2016 via email

@kcooney
Copy link
Member Author

kcooney commented Dec 15, 2016

So @panchenko you are proposing keeping both expectThrows() and assertThrows() as-is?

I wasn't aware that the same methods were added to TestNG. Perhaps we should reach out to the TestNG team to see if the methods they had led to any confusion, and if not, keep both methods (at least in org.junit.Assert; JUnit 5 may do something different).

@panchenko
Copy link
Contributor

@kcooney exactly, I would suggest keeping both.

scalatest also has 2 methods - assertThrows and intercept.

@jbduncan
Copy link

jbduncan commented May 1, 2017

I disagree that expectThrows should be included.

Looking at TestNG's source code (and remembering the source code from older versions of JUnit 5), it looks to me that expectThrows is merely a wrapper around assertThrows that returns void instead of T extends Throwable, so it makes no sense to me to have expectThrows from both a performance and readability standpoint, since the return result of assertThrows can always be ignored.

@kcooney
Copy link
Member Author

kcooney commented May 1, 2017

@jbduncan actually assertThrows is a wrapper around expectThrows.

I don't follow the performance concerns. These methods are passed lamdas which throw exceptions. Exception handling is much more expensive than a function call.

JUnit4 doesn't have any assert methods than return something that should be used. I am not sure we should start now, so I think we should have both, just like TestNG.

@kcooney kcooney closed this May 1, 2017
@kcooney kcooney removed the 4.13 label May 1, 2017
@jbduncan
Copy link

jbduncan commented May 1, 2017

@kcooney Oh yes, silly me! Got them a bit mixed in my mind.

@panchenko panchenko mentioned this pull request Feb 8, 2018
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.

6 participants