public void testNullsOnReturnValues_returnTypeFiltered() throws Exception { try { tester .forAllPublicStaticMethods(BadNullsFactory.class) .thatReturn(Iterable.class) .testNulls(); } catch (AssertionFailedError expected) { assertThat(expected) .hasMessageThat() .isEqualTo( "No public static methods that return java.lang.Iterable or subtype are found in " + BadNullsFactory.class + "."); return; } fail(); }
public void testForAllPublicStaticMethods_noPublicStaticMethods() throws Exception { try { tester.forAllPublicStaticMethods(NoPublicStaticMethods.class).testEquals(); } catch (AssertionFailedError expected) { assertThat(expected) .hasMessageThat() .isEqualTo( "No public static methods that return java.lang.Object or subtype are found in " + NoPublicStaticMethods.class + "."); return; } fail(); }
/** * Returns an object responsible for performing sanity tests against the return values of all * public static methods declared by {@code cls}, excluding superclasses. */ public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) { ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder(); for (Method method : cls.getDeclaredMethods()) { Invokable<?, ?> invokable = Invokable.from(method); invokable.setAccessible(true); if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) { builder.add(invokable); } } return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods"); }
for (Invokable<?, ?> factory : getFactoriesToTest()) { try { testEqualsUsing(factory);
for (Invokable<?, ?> factory : getFactoriesToTest()) { try { testEqualsUsing(factory);
/** * Tests null checks against the instance methods of the return values, if any. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testNulls() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null && packagesToTest.contains(Reflection.getPackageName(instance.getClass()))) { try { nullPointerTester.testAllPublicInstanceMethods(instance); } catch (AssertionError e) { AssertionError error = new AssertionFailedError( "Null check failed on return value of " + factory); error.initCause(e); throw error; } } } return this; }
/** * Tests null checks against the instance methods of the return values, if any. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testNulls() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null && packagesToTest.contains(Reflection.getPackageName(instance.getClass()))) { try { nullPointerTester.testAllPublicInstanceMethods(instance); } catch (AssertionError e) { AssertionError error = new AssertionFailedError("Null check failed on return value of " + factory); error.initCause(e); throw error; } } } return this; }
/** * Runs serialization test on the return values of the static methods. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testSerializable() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null) { try { SerializableTester.reserialize(instance); } catch (RuntimeException e) { AssertionError error = new AssertionFailedError("Serialization failed on return value of " + factory); error.initCause(e.getCause()); throw error; } } } return this; }
/** * Runs serialization test on the return values of the static methods. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testSerializable() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null) { try { SerializableTester.reserialize(instance); } catch (RuntimeException e) { AssertionError error = new AssertionFailedError( "Serialization failed on return value of " + factory); error.initCause(e.getCause()); throw error; } } } return this; }
/** * Tests {@link Object#equals} and {@link Object#hashCode} against the return values of the * static methods, by asserting that when equal parameters are passed to the same static method, * the return value should also be equal; and vice versa. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testEquals() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { try { testEqualsUsing(factory); } catch (FactoryMethodReturnsNullException e) { // If the factory returns null, we just skip it. } } return this; }
/** * Tests {@link Object#equals} and {@link Object#hashCode} against the return values of the * static methods, by asserting that when equal parameters are passed to the same static method, * the return value should also be equal; and vice versa. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testEquals() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { try { testEqualsUsing(factory); } catch (FactoryMethodReturnsNullException e) { // If the factory returns null, we just skip it. } } return this; }
public void testNullsOnReturnValues_returnTypeFiltered() throws Exception { try { tester .forAllPublicStaticMethods(BadNullsFactory.class) .thatReturn(Iterable.class) .testNulls(); } catch (AssertionFailedError expected) { assertThat(expected) .hasMessage( "No public static methods that return java.lang.Iterable or subtype are found in " + BadNullsFactory.class + "."); return; } fail(); }
public void testSerializableOnReturnValues_good() throws Exception { tester.forAllPublicStaticMethods(GoodSerializableFactory.class).testSerializable(); }
public void testEqualsForReturnValues_factoryReturnsNullAndAnnotated() throws Exception { tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class).testEquals(); }
/** * Returns an object responsible for performing sanity tests against the return values * of all public static methods declared by {@code cls}, excluding superclasses. */ public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) { ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder(); for (Method method : cls.getDeclaredMethods()) { Invokable<?, ?> invokable = Invokable.from(method); invokable.setAccessible(true); if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) { builder.add(invokable); } } return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods"); }
public void testSerializableForReturnValues_factoryReturnsNullAndAnnotated() throws Exception { tester.forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class) .testSerializable(); }
public void testNullsOnReturnValues_returnTypeFiltered() throws Exception { try { tester .forAllPublicStaticMethods(BadNullsFactory.class) .thatReturn(Iterable.class) .testNulls(); } catch (AssertionFailedError expected) { assertEquals( "No public static methods that return java.lang.Iterable or subtype are found in " + BadNullsFactory.class + ".", expected.getMessage()); return; } fail(); }
/** * Returns an object responsible for performing sanity tests against the return values of all * public static methods declared by {@code cls}, excluding superclasses. */ public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) { ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder(); for (Method method : cls.getDeclaredMethods()) { Invokable<?, ?> invokable = Invokable.from(method); invokable.setAccessible(true); if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) { builder.add(invokable); } } return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods"); }
public void testHashCode_equalsAndSerializable() throws Exception { sanityTester().testEqualsAndSerializable(); }
public void testReturnValuesFromAnotherPackageIgnoredForNullTests() throws Exception { new ClassSanityTester().forAllPublicStaticMethods(JdkObjectFactory.class).testNulls(); }