dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

@NonFinalForTesting #1208

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Effective Java recommends to make classes final, unless designed for extension. 
Unfortunately, for ease of mocking during unit testing, it is sometimes 
necessary to make classes/methods non-final.

Having an annotation, @NonFinalForTesting to indicate that a method/class had 
been made non-final for the purposes of testing, in a similar vein to 
@VisibleForTesting.

I am aware of the following alternatives:
    * Use Powermock, which can mock final methods/classes - I tend to not bother, because it has an ugly API, clutters tests, and end up just making my code non-final.
    * Writing my own annotation - I often have do this, and suspect others do this, or write a comment (urgh)

Original issue reported on code.google.com by thomasfi...@gmail.com on 20 Nov 2012 at 9:29

GoogleCodeExporter commented 9 years ago
I'm not sure why we'd add this if we weren't also planning to use it in Guava 
itself?

Original comment by lowas...@google.com on 20 Nov 2012 at 9:32

GoogleCodeExporter commented 9 years ago
Presumably that criteria ("used in Guava itself") doesn't hold for all new 
features/code. Why should annotations be treated differently? The annotation 
may still be useful for other developers.

I appreciate Guava high power-to-weight ratio, so if this annotation doesn't 
have widespread appeal, I accept it shouldn't be included in Guava.

Original comment by thomasfi...@gmail.com on 25 Nov 2012 at 10:05

GoogleCodeExporter commented 9 years ago
I am but one person, but this annotation sure does appeal to me. Would use it 
if made available.

Original comment by stephan...@gmail.com on 26 Nov 2012 at 6:13

GoogleCodeExporter commented 9 years ago
The need to subclass a class for testing purposes that could have been final 
otherwise is a pretty strong design smell to me. We doubt an annotation like 
this is necessary, and we'd have to believe very *strongly in favor* of it to 
want to add it to Guava.

Original comment by kevinb@google.com on 28 Nov 2012 at 1:30

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08