After a few reading and questions like this one I was wondering if there was a point in using @NonNull
Android Support Annotation.
I can see a very small warning from Android Studio if I attempt to call a method with a null parameter that is annotated as @NonNull. Just a warning??
What about Unit Testing ? Should I test the method with a null paramter? If I do ... I will get a NullPointerException
and my test will fail.
Let's say we are two developers. One working on the API and one testing the API in all kind of manners. As the second developer, it's my responsibility to test everything so that the API is bullet-proof. That's the whole point of Unit Testing, right ?
So then ... what's the point of the first developer using @NonNull ?
If someone else were to use this API with a null parameter ... then the API would throw a NPE. He then would think : "Urg, that API sucks... NPE !" and he would be right. That naughty dev for not checking if the parameter he sent is null or not should be faced with an IllegalArgumentException because it's his fault not that of the API's!
Am I wrong ?
I thought these annotations would enforce the compiler to show errors like attempting to call methodName(@NonNull Object object) with null parameter
.
Update 1
Alright thank you all for your comments. I'd like to summ up if possible regarding the "issue" I am facing here. In an abstract way.
I write some code (an API, a Library, a Class, whatever) with private internal code wrapped by public methods that provides features.
Assume these public methods will be used by anyone else (including me). Some of them take arguments that must never be null or else all hell breaks loose.
Reading your comments, I am facing the following options :
- Keep on using contracts / annotations (
@NonNull
) supported by Java Documentation stipulatingparameter must not be null
. Don't check for null parameters (or else IDE will warn me) and, somehow, pray that I will never receive a null parameter ; - Same as above, but enforce a null check (even though IDE will warn that
condition will always be false
) and throwIllegalArgumentException
instead of causing an NPE in case I receive a null parameter ; - Stop using contracts / annotations, use Java Doc to warn others dev and add manual check for all parameters.
Finally, for unit testing ... I know I can not have bullet-proof code, but I like to "monkey-test" my code as much as possible to prevent unexpected behavior from my code as well to validate the process (which is at the base of Unit Testing, I believe)-