{"record":{"id":"23028c1e603c16c4","repo":"google/guava","slug":"s-returns-null-and-cannot-be-used-to-test-instanc","errorCode":null,"errorMessage":"%s returns null and cannot be used to test instance methods.","messagePattern":"(.+?) returns null and cannot be used to test instance methods\\.","errorType":"exception","errorClass":"FactoryMethodReturnsNullException","httpStatus":null,"severity":"error","filePath":"android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java","lineNumber":771,"sourceCode":"    if (defaultValue != null) {\n      return defaultValue;\n    }\n    @SuppressWarnings(\"unchecked\") // ArbitraryInstances always returns generics-safe dummies.\n    T value = (T) ArbitraryInstances.get(rawType);\n    if (value != null) {\n      return value;\n    }\n    if (rawType.isInterface()) {\n      return new SerializableDummyProxy(this).newProxy(type);\n    }\n    return null;\n  }\n\n  private static <T> T createInstance(Invokable<?, ? extends T> factory, List<?> args)\n      throws FactoryMethodReturnsNullException, InvocationTargetException, IllegalAccessException {\n    T instance = invoke(factory, args);\n    if (instance == null) {\n      throw new FactoryMethodReturnsNullException(factory);\n    }\n    return instance;\n  }\n\n  private static <T> @Nullable T invoke(Invokable<?, ? extends T> factory, List<?> args)\n      throws InvocationTargetException, IllegalAccessException {\n    T returnValue = factory.invoke(null, args.toArray());\n    if (returnValue == null) {\n      Assert.assertTrue(\n          factory + \" returns null but it's not annotated with @Nullable\", isNullable(factory));\n    }\n    return returnValue;\n  }\n\n  /**\n   * Thrown if the test tries to invoke a constructor or static factory method but failed because\n   * the dummy value of a constructor or method parameter is unknown.\n   */","sourceCodeStart":753,"sourceCodeEnd":789,"githubUrl":"https://github.com/google/guava/blob/94f39958baf7ad51ddf9c70e406ed6b188194daa/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java#L753-L789","documentation":"ClassSanityTester auto-generates instances of a class (via constructors or static factory methods) so it can exercise equals/hashCode/toString and null checks. It calls createInstance(), which invokes the factory through invoke(); if the call returns null, a FactoryMethodReturnsNullException is thrown because a null instance cannot be used to test instance methods. The accompanying invoke() assert also requires any null-returning factory to be annotated @Nullable.","triggerScenarios":"Calling ClassSanityTester.doTestEquals(Class), doTestNulls(Class), doTestSerializableEqualityInterfaces(Class), or any method that needs a live instance, on a type whose only factory/constructor returns null for the dummy argument values the tester generated.","commonSituations":"Testing a class whose static factory returns null for some inputs (e.g. caching/interning factories, Optional-style wrappers); factories legitimately annotated @Nullable; the tester could not synthesize non-null values for all parameters so it fell through to a null-returning path.","solutions":["Register non-null dummy values for the factory's parameters with tester.setDefault(type, value) or tester.setDistinctValues(type, v1, v2) so the factory returns a real instance.","Provide an explicit instance via the tester's set API (e.g. tester.set(containerClass, instance)) or point it at a factory that never returns null for the test inputs.","If the null return is intentional, annotate the factory method with @Nullable so invoke() tolerates it, and supply a second non-null-returning factory for instance-level tests.","Exclude the null-returning factory from instance-method testing and test those methods separately with a hand-built instance."],"exampleFix":"// before: factory returns null for default args\ntester.doTestEquals(MyClass.class);\n// FactoryMethodReturnsNullException: of(...) returns null...\n\n// after: supply a value so the factory returns a real instance\ntester.setDefault(Config.class, Config.defaults())\n      .doTestEquals(MyClass.class);","handlingStrategy":"validation","validationCode":"// Before relying on ClassSanityTester, ensure every factory it will invoke\n// returns non-null for the dummy values; pre-register those values.\nClassSanityTester tester = new ClassSanityTester();\nfor (Invokable<?,?> f : factoriesUnderTest) {\n  Object probe = safeInvoke(f, dummyArgs); // your helper\n  if (probe == null) {\n    tester.setDefault(/* param type */, knownNonNullValue);\n  }\n}","typeGuard":null,"tryCatchPattern":"// FactoryMethodReturnsNullException is checked; catch it where you drive the tester\n// only to skip a class, not to mask a real problem.\ntry {\n  tester.doTestEquals(MyClass.class);\n} catch (FactoryMethodReturnsNullException e) {\n  // supply custom values and retry, or exclude the class\n  throw AssumptionViolatedException? // skip via JUnit assumeNoException(e)\n}","preventionTips":["Annotate null-returning factories with @Nullable so the tester's invoke() tolerates them.","Register distinct non-null dummy values for every parameter type the factory needs.","Prefer testing instance methods with a hand-constructed instance when factories may return null.","Run ClassSanityTester in isolation first to see which factory it picks before relying on auto-detection."],"tags":["guava","testing","class-sanity","instantiation","null"],"backgroundTag":null,"analyzedSha":"94f39958baf7ad51ddf9c70e406ed6b188194daa","analyzedAt":"2026-08-13T22:50:30.265Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}