Package com.google.common.testing
Class NullPointerTester
- java.lang.Object
-
- com.google.common.testing.NullPointerTester
-
@Beta @GwtIncompatible public final class NullPointerTester extends java.lang.Object
A test utility that verifies that your methods and constructors throwNullPointerExceptionorUnsupportedOperationExceptionwhenever null is passed to a parameter that isn't annotated withNullable.The tested methods and constructors are invoked -- each time with one parameter being null and the rest not null -- and the test fails if no expected exception is thrown.
NullPointerTesteruses best effort to pick non-null default values for many common JDK and Guava types, and also for interfaces and public classes that have public parameter-less constructors. When the non-null default value for a particular parameter type cannot be provided byNullPointerTester, the caller can provide a custom non-null default value for the parameter type viasetDefault(java.lang.Class<T>, T).- Since:
- 10.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNullPointerTester.VisibilityVisibility of any method or constructor.
-
Constructor Summary
Constructors Constructor Description NullPointerTester()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NullPointerTesterignore(java.lang.reflect.Method method)Ignoremethodin the tests that follow.<T> NullPointerTestersetDefault(java.lang.Class<T> type, T value)Sets a default value that can be used for any parameter of typetype.voidtestAllPublicConstructors(java.lang.Class<?> c)RunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc.voidtestAllPublicInstanceMethods(java.lang.Object instance)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public instance method of the class ofinstance, including those inherited from superclasses of the same package.voidtestAllPublicStaticMethods(java.lang.Class<?> c)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public static method of classc, including those "inherited" from superclasses of the same package.voidtestConstructor(java.lang.reflect.Constructor<?> ctor)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-Nullableparameters are null.voidtestConstructorParameter(java.lang.reflect.Constructor<?> ctor, int paramIndex)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestConstructors(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility.voidtestInstanceMethods(java.lang.Object instance, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every instance method of the class ofinstancewith at leastminimalVisibility, including those inherited from superclasses of the same package.voidtestMethod(java.lang.Object instance, java.lang.reflect.Method method)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-Nullableparameters are null.voidtestMethodParameter(java.lang.Object instance, java.lang.reflect.Method method, int paramIndex)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestStaticMethods(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every static method of classcthat has at leastminimalVisibility, including those "inherited" from superclasses of the same package.
-
-
-
Method Detail
-
setDefault
public <T> NullPointerTester setDefault(java.lang.Class<T> type, T value)
Sets a default value that can be used for any parameter of typetype. Returns this object.
-
ignore
public NullPointerTester ignore(java.lang.reflect.Method method)
Ignoremethodin the tests that follow. Returns this object.- Since:
- 13.0
-
testConstructors
public void testConstructors(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility.
-
testAllPublicConstructors
public void testAllPublicConstructors(java.lang.Class<?> c)
RunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc.
-
testStaticMethods
public void testStaticMethods(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every static method of classcthat has at leastminimalVisibility, including those "inherited" from superclasses of the same package.
-
testAllPublicStaticMethods
public void testAllPublicStaticMethods(java.lang.Class<?> c)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public static method of classc, including those "inherited" from superclasses of the same package.
-
testInstanceMethods
public void testInstanceMethods(java.lang.Object instance, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every instance method of the class ofinstancewith at leastminimalVisibility, including those inherited from superclasses of the same package.
-
testAllPublicInstanceMethods
public void testAllPublicInstanceMethods(java.lang.Object instance)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public instance method of the class ofinstance, including those inherited from superclasses of the same package.
-
testMethod
public void testMethod(@Nullable java.lang.Object instance, java.lang.reflect.Method method)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-Nullableparameters are null.- Parameters:
instance- the instance to invokemethodon, or null ifmethodis static
-
testConstructor
public void testConstructor(java.lang.reflect.Constructor<?> ctor)
Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-Nullableparameters are null.
-
testMethodParameter
public void testMethodParameter(@Nullable java.lang.Object instance, java.lang.reflect.Method method, int paramIndex)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is markedNullable, this method does nothing.- Parameters:
instance- the instance to invokemethodon, or null ifmethodis static
-
testConstructorParameter
public void testConstructorParameter(java.lang.reflect.Constructor<?> ctor, int paramIndex)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is markedNullable, this method does nothing.
-
-