Test Null

TestNull is a TestingPatterns.

Invoke a method with a null parameter. If the method is not meant to be invoked with a null parameter, is the effect of doing so documented? Does the invocation actually have that effect?


Interesting point: - invoke the method with a null parameter where the method does not expect to have a null parameter, you get… guess what?, a NullPointerException. Which is the reason why NullPointerException exist? If/Where a NullPointerException should fail you the whole application is the real question.


There are many ways of getting a null value when you don't want one and don't expect one, so there's a reasonable chance that sooner or later your code will be invoked with a bad parameter. When this happens, it's important for your code to pass the blame onto the calling code by acting sensibly :-).


Isn't this better handled by assertions? -- abo

... assuming of course that you have a means for implementing assertions, and that you always leave them enabled. --

No, it's better handled by exceptions. Just code as if null references couldn't happen. Most environments will throw an exception for you if you try to use a null reference, so all you have to do is handle null reference exceptions (or exceptions in general) properly and you ought to be fine. -- PhilGoodwin

Sun assert guideline says don't use assert to check parameters. http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html#usage -- Jtf


See also NullObject


Keep in mind, a null pointer is not a truly bad pointer. Suppose an object's constructor assigns some global variable to this then throws an exception. I have worse schemes in my head if you want them.


CategoryTesting CategoryNull


EditText of this page (last edited April 10, 2012) or FindPage with title or text search