Class NullPointerTester

java.lang.Object
com.google.common.testing.NullPointerTester

@GwtIncompatible @NullMarked public final class NullPointerTester extends Object
A test utility that verifies that your methods and constructors throw NullPointerException or UnsupportedOperationException whenever null is passed to a parameter whose declaration or type isn't annotated with an annotation with the simple name Nullable, CheckForNull, NullableType, or NullableDecl.

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 by NullPointerTester, the caller can provide a custom non-null default value for the parameter type via setDefault(Class, T).

Since:
10.0
Author:
Kevin Bourrillion
  • Constructor Details

    • NullPointerTester

      public NullPointerTester()
      Constructs a new NullPointerTester.
  • Method Details

    • setDefault

      @CanIgnoreReturnValue public <T> NullPointerTester setDefault(Class<T> type, T value)
      Sets a default value that can be used for any parameter of type type. Returns this object.
    • ignore

      @CanIgnoreReturnValue public NullPointerTester ignore(Method method)
      Ignore method in the tests that follow. Returns this object.
      Since:
      13.0
    • ignore

      @CanIgnoreReturnValue public NullPointerTester ignore(Constructor<?> constructor)
      Ignore constructor in the tests that follow. Returns this object.
      Since:
      22.0
    • testConstructors

      public void testConstructors(Class<?> c, NullPointerTester.Visibility minimalVisibility)
      Runs testConstructor(Constructor) on every constructor in class c that has at least minimalVisibility.
    • testAllPublicConstructors

      public void testAllPublicConstructors(Class<?> c)
      Runs testConstructor(Constructor) on every public constructor in class c.
    • testStaticMethods

      public void testStaticMethods(Class<?> c, NullPointerTester.Visibility minimalVisibility)
      Runs testMethod(Object, Method) on every static method of class c that has at least minimalVisibility, including those "inherited" from superclasses of the same package.
    • testAllPublicStaticMethods

      public void testAllPublicStaticMethods(Class<?> c)
      Runs testMethod(Object, Method) on every public static method of class c, including those "inherited" from superclasses of the same package.
    • testInstanceMethods

      public void testInstanceMethods(Object instance, NullPointerTester.Visibility minimalVisibility)
      Runs testMethod(Object, Method) on every instance method of the class of instance with at least minimalVisibility, including those inherited from superclasses of the same package.
    • testAllPublicInstanceMethods

      public void testAllPublicInstanceMethods(Object instance)
      Runs testMethod(Object, Method) on every public instance method of the class of instance, including those inherited from superclasses of the same package.
    • testMethod

      public void testMethod(@Nullable Object instance, Method method)
      Verifies that method produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null.
      Parameters:
      instance - the instance to invoke method on, or null if method is static
    • testConstructor

      public void testConstructor(Constructor<?> ctor)
      Verifies that ctor produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null.
    • testMethodParameter

      public void testMethodParameter(@Nullable Object instance, Method method, int paramIndex)
      Verifies that method produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If this parameter is marked nullable, this method does nothing.
      Parameters:
      instance - the instance to invoke method on, or null if method is static
    • testConstructorParameter

      public void testConstructorParameter(Constructor<?> ctor, int paramIndex)
      Verifies that ctor produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If this parameter is marked nullable, this method does nothing.