com.google.common.testing
Class AbstractPackageSanityTests

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by com.google.common.testing.AbstractPackageSanityTests
All Implemented Interfaces:
junit.framework.Test

@Beta
public abstract class AbstractPackageSanityTests
extends junit.framework.TestCase

Automatically runs sanity checks against top level classes in the same package of the test that extends AbstractPackageSanityTests. Currently sanity checks include NullPointerTester, EqualsTester and SerializableTester. For example:

 public class PackageSanityTests extends AbstractPackageSanityTests {}
 

Note that only top-level classes with either a non-private constructor or a non-private static factory method to construct instances can have their instance methods checked. For example:

 public class Address {
   private final String city;
   private final String state;
   private final String zipcode;

   public Address(String city, String state, String zipcode) {...}

   @Override public boolean equals(Object obj) {...}
   @Override public int hashCode() {...}
   ...
 }
 
No cascading checks are performed against the return values of methods unless the method is a static factory method. Neither are semantics of mutation methods such as someList.add(obj) checked. For more detailed discussion of supported and unsupported cases, see testEquals(), testNulls() and testSerializable().

For testing against the returned instances from a static factory class, such as

 interface Book {...}
 public class Books {
   public static Book hardcover(String title) {...}
   public static Book paperback(String title) {...}
 }
 
please use ClassSanityTester.forAllPublicStaticMethods(java.lang.Class).

This class incurs IO because it scans the classpath and reads classpath resources.

Since:
14.0
Author:
Ben Yu

Constructor Summary
AbstractPackageSanityTests()
           
 
Method Summary
protected  void ignoreClasses(com.google.common.base.Predicate<? super Class<?>> condition)
          Specifies that classes that satisfy the given predicate aren't tested for sanity.
protected  void publicApiOnly()
          Restricts the sanity tests for public API only.
protected
<T> void
setDefault(Class<T> type, T value)
          Sets the default value for type, when dummy value for a parameter of the same type needs to be created in order to invoke a method or constructor.
 void testEquals()
          Tests equals() and hashCode() implementations for every top-level public class in the package, that explicitly implements Object.equals(java.lang.Object).
 void testNulls()
          Performs NullPointerTester checks for all top-level public classes in the package.
 void testSerializable()
          Tests all top-level public Serializable classes in the package.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractPackageSanityTests

public AbstractPackageSanityTests()
Method Detail

publicApiOnly

protected final void publicApiOnly()
Restricts the sanity tests for public API only. By default, package-private API are also covered.


testSerializable

public void testSerializable()
                      throws Exception
Tests all top-level public Serializable classes in the package. For a serializable Class C: In all cases, if C needs custom logic for testing serialization, you can add an explicit testSerializable() test in the corresponding CTest class, and C will be excluded from automated serialization test performed by this method.

Throws:
Exception

testNulls

public void testNulls()
               throws Exception
Performs NullPointerTester checks for all top-level public classes in the package. For a class C In all cases, if C needs custom logic for testing nulls, you can add an explicit testNulls() test in the corresponding CTest class, and C will be excluded from the automated null tests performed by this method.

Throws:
Exception

testEquals

public void testEquals()
                throws Exception
Tests equals() and hashCode() implementations for every top-level public class in the package, that explicitly implements Object.equals(java.lang.Object). For a class C: In all cases, if C needs custom logic for testing equals(), you can add an explicit testEquals() test in the corresponding CTest class, and C will be excluded from the automated equals test performed by this method.

Throws:
Exception

setDefault

protected final <T> void setDefault(Class<T> type,
                                    T value)
Sets the default value for type, when dummy value for a parameter of the same type needs to be created in order to invoke a method or constructor. The default value isn't used in testing Object.equals(java.lang.Object) because more than one sample instances are needed for testing inequality.


ignoreClasses

protected final void ignoreClasses(com.google.common.base.Predicate<? super Class<?>> condition)
Specifies that classes that satisfy the given predicate aren't tested for sanity.



Copyright © 2010-2013. All Rights Reserved.