我想为我的方法参数执行空值检查,比如参数不应该为null。在我的Java代码中使用类似这样的代码assertNotNull("Map should not be null", filePaths);
可以吗?
if(filePaths == null){
throw new IllegalArgumentException("Maps cannot be null");
}
为了让我的代码不受所有空检查的干扰,我想写一个自己的Validator
类,并重载notNull
方法。但是有没有现成且简单易用的东西可以避免重新发明轮子。
我唯一担心使用JUnit
的Assert
是它会抛出AssertionError
而不是IllegalArgumentException
等。
Preconditions
? - Louis WassermanGuava
,不过看起来值得一试。 - Charu Khurana