com.google.common.testing
Class ClassSanityTester

java.lang.Object
  extended by com.google.common.testing.ClassSanityTester

@Beta
public final class ClassSanityTester
extends Object

Tester that runs automated sanity tests for any given class. A typical use case is to test static factory classes like:

 interface Book {...}
 public class Books {
   public static Book hardcover(String title) {...}
   public static Book paperback(String title) {...}
 }
 
And all the created Book instances can be tested with:
 new ClassSanityTester()
     .forAllPublicStaticMethods(Books.class)
     .thatReturn(Book.class)
     .testEquals(); // or testNulls(), testSerializable() etc.
 

Since:
14.0
Author:
Ben Yu

Nested Class Summary
 class ClassSanityTester.FactoryMethodReturnValueTester
          Runs sanity tests against return values of static factory methods declared by a class.
 
Constructor Summary
ClassSanityTester()
           
 
Method Summary
 ClassSanityTester.FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls)
          Returns an object responsible for performing sanity tests against the return values of all public static methods declared by cls, excluding superclasses.
<T> ClassSanityTester
setDefault(Class<T> type, T value)
          Sets the default value for type.
<T> ClassSanityTester
setSampleInstances(Class<T> type, Iterable<? extends T> instances)
          Sets sample instances for type for purpose of equals testing, where different values are needed to test inequality.
 void testEquals(Class<?> cls)
          Tests the Object.equals(java.lang.Object) and Object.hashCode() of cls.
 void testNulls(Class<?> cls)
          Tests that cls properly checks null on all constructor and method parameters that aren't annotated with Nullable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassSanityTester

public ClassSanityTester()
Method Detail

setDefault

public <T> ClassSanityTester setDefault(Class<T> type,
                                        T value)
Sets the default value for type. The default value isn't used in testing Object.equals(java.lang.Object) because more than one sample instances are needed for testing inequality. To set sample instances for equality testing, use setSampleInstances(java.lang.Class, java.lang.Iterable) instead.


setSampleInstances

public <T> ClassSanityTester setSampleInstances(Class<T> type,
                                                Iterable<? extends T> instances)
Sets sample instances for type for purpose of equals testing, where different values are needed to test inequality.

Used for types that ClassSanityTester doesn't already know how to sample. It's usually necessary to add two unequal instances for each type, with the exception that if the sample instance is to be passed to a Nullable parameter, one non-null sample is sufficient. Setting an empty list will clear sample instances for type.


testNulls

public void testNulls(Class<?> cls)
Tests that cls properly checks null on all constructor and method parameters that aren't annotated with Nullable. In details:


testEquals

public void testEquals(Class<?> cls)
Tests the Object.equals(java.lang.Object) and Object.hashCode() of cls. In details:

Note that constructors taking a builder object cannot be tested effectively because semantics of builder can be arbitrarily complex. Still, a factory class can be created in the test to facilitate equality testing. For example:

 public class FooTest {

   private static class FooFactoryForTest {
     public static Foo create(String a, String b, int c, boolean d) {
       return Foo.builder()
           .setA(a)
           .setB(b)
           .setC(c)
           .setD(d)
           .build();
     }
   }

   public void testEquals() {
     new ClassSanityTester()
       .forAllPublicStaticMethods(FooFactoryForTest.class)
       .thatReturn(Foo.class)
       .testEquals();
   }
 }
 
It will test that Foo objects created by the create(a, b, c, d) factory method with equal parameters are equal and vice versa, thus indirectly tests the builder equality.


forAllPublicStaticMethods

public ClassSanityTester.FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls)
Returns an object responsible for performing sanity tests against the return values of all public static methods declared by cls, excluding superclasses.



Copyright © 2010-2013. All Rights Reserved.