14

I want to do null checks for my method arguments, like parameters should not be null. Is it okay to use something like this assertNotNull("Map should not be null", filePaths); in my Java code? I'm trying to avoid

if(filePaths == null){
  throw new IllegalArgumentException("Maps cannot be null");
}

just to keep my code clean from all those null checks. I know I can write a Validator class of my own and have overloaded notNull methods but is there something existing and simple to use to not re-invent the wheel.

The only drawback I see of using JUnit Assert is that it throws AssertionError and not IllegalArgumentException and so forth.

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
  • 3
    Why not use Guava's [`Preconditions`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html)? – Louis Wasserman Jul 12 '13 at 17:26
  • Another option is to use the assert keyword provided by the Java language -- e.g., assert null != filepaths : "Maps cannot be null". These checks can be turned on or off on startup. – Andy Thomas Jul 12 '13 at 17:28
  • 2
    @AndyThomas: _Do not use assertions for argument checking in public methods._ from [Documentation](http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html). If assertions are disabled, it will never be called – Charu Khurana Jul 12 '13 at 17:32
  • @LouisWasserman: Thanks for your advice, we are not currently using `Guava`, but it looks like worth giving shot – Charu Khurana Jul 12 '13 at 17:33
  • @Learner - Good point. In some special cases I've used assert() for non-public methods. But usually it would be better to stick to a single approach, like Louis' suggestion of Guava's Preconditions. – Andy Thomas Jul 12 '13 at 18:45

2 Answers2

13

If you use Java 7+, you can use:

Objects.requireNonNull(filePaths, "Map should not be null");

Also with a null argument, I would expect a NullPointerException or an IllegalArgumentException, but not an AssertionError.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks for your response. We re still on Java 6 but good to know that its in built in Java 7 – Charu Khurana Jul 12 '13 at 17:34
  • @Learner With Java 6 you can use Guava Preconditions as already pointed out (it does roughly the same thing as Objects.requireNonNull). – assylias Jul 12 '13 at 17:45
11

No, it is not OK to use it. The JUnit assert methods throw an AssertionError. It's not a good idea to throw Error in production code. From the javadoc:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171