Package com.google.common.testing
Class NullPointerTester
- java.lang.Object
-
- com.google.common.testing.NullPointerTester
-
@Beta @GwtIncompatible public final class NullPointerTester extends Object
A test utility that verifies that your methods and constructors throwNullPointerException
orUnsupportedOperationException
whenever null is passed to a parameter whose declaration or type isn't annotated with an annotation with the simple nameNullable
,CheckForNull
,NullableType
, orNullableDecl
.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.
NullPointerTester
uses 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
- Author:
- Kevin Bourrillion
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NullPointerTester.Visibility
Visibility of any method or constructor.
-
Constructor Summary
Constructors Constructor Description NullPointerTester()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NullPointerTester
ignore(Constructor<?> constructor)
Ignoreconstructor
in the tests that follow.NullPointerTester
ignore(Method method)
Ignoremethod
in the tests that follow.<T> NullPointerTester
setDefault(Class<T> type, T value)
Sets a default value that can be used for any parameter of typetype
.void
testAllPublicConstructors(Class<?> c)
RunstestConstructor(java.lang.reflect.Constructor<?>)
on every public constructor in classc
.void
testAllPublicInstanceMethods(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.void
testAllPublicStaticMethods(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.void
testConstructor(Constructor<?> ctor)
Verifies thatctor
produces aNullPointerException
orUnsupportedOperationException
whenever any of its non-nullable parameters are null.void
testConstructorParameter(Constructor<?> ctor, int paramIndex)
Verifies thatctor
produces aNullPointerException
orUnsupportedOperationException
when the parameter in positionparamIndex
is null.void
testConstructors(Class<?> c, NullPointerTester.Visibility minimalVisibility)
RunstestConstructor(java.lang.reflect.Constructor<?>)
on every constructor in classc
that has at leastminimalVisibility
.void
testInstanceMethods(Object instance, NullPointerTester.Visibility minimalVisibility)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)
on every instance method of the class ofinstance
with at leastminimalVisibility
, including those inherited from superclasses of the same package.void
testMethod(@Nullable Object instance, Method method)
Verifies thatmethod
produces aNullPointerException
orUnsupportedOperationException
whenever any of its non-nullable parameters are null.void
testMethodParameter(@Nullable Object instance, Method method, int paramIndex)
Verifies thatmethod
produces aNullPointerException
orUnsupportedOperationException
when the parameter in positionparamIndex
is null.void
testStaticMethods(Class<?> c, NullPointerTester.Visibility minimalVisibility)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)
on every static method of classc
that has at leastminimalVisibility
, including those "inherited" from superclasses of the same package.
-
-
-
Constructor Detail
-
NullPointerTester
public NullPointerTester()
-
-
Method Detail
-
setDefault
public <T> NullPointerTester setDefault(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(Method method)
Ignoremethod
in the tests that follow. Returns this object.- Since:
- 13.0
-
ignore
public NullPointerTester ignore(Constructor<?> constructor)
Ignoreconstructor
in the tests that follow. Returns this object.- Since:
- 22.0
-
testConstructors
public void testConstructors(Class<?> c, NullPointerTester.Visibility minimalVisibility)
RunstestConstructor(java.lang.reflect.Constructor<?>)
on every constructor in classc
that has at leastminimalVisibility
.
-
testAllPublicConstructors
public void testAllPublicConstructors(Class<?> c)
RunstestConstructor(java.lang.reflect.Constructor<?>)
on every public constructor in classc
.
-
testStaticMethods
public void testStaticMethods(Class<?> c, NullPointerTester.Visibility minimalVisibility)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)
on every static method of classc
that has at leastminimalVisibility
, including those "inherited" from superclasses of the same package.
-
testAllPublicStaticMethods
public void testAllPublicStaticMethods(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(Object instance, NullPointerTester.Visibility minimalVisibility)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)
on every instance method of the class ofinstance
with at leastminimalVisibility
, including those inherited from superclasses of the same package.
-
testAllPublicInstanceMethods
public void testAllPublicInstanceMethods(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 Object instance, Method method)
Verifies thatmethod
produces aNullPointerException
orUnsupportedOperationException
whenever any of its non-nullable parameters are null.- Parameters:
instance
- the instance to invokemethod
on, or null ifmethod
is static
-
testConstructor
public void testConstructor(Constructor<?> ctor)
Verifies thatctor
produces aNullPointerException
orUnsupportedOperationException
whenever any of its non-nullable parameters are null.
-
testMethodParameter
public void testMethodParameter(@Nullable Object instance, Method method, int paramIndex)
Verifies thatmethod
produces aNullPointerException
orUnsupportedOperationException
when the parameter in positionparamIndex
is null. If this parameter is marked nullable, this method does nothing.- Parameters:
instance
- the instance to invokemethod
on, or null ifmethod
is static
-
testConstructorParameter
public void testConstructorParameter(Constructor<?> ctor, int paramIndex)
Verifies thatctor
produces aNullPointerException
orUnsupportedOperationException
when the parameter in positionparamIndex
is null. If this parameter is marked nullable, this method does nothing.
-
-