|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjunit.framework.Assert
junit.framework.TestCase
com.google.common.testing.AbstractPackageSanityTests
@Beta public abstract class AbstractPackageSanityTests
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.
| 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
|
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 |
|---|
public AbstractPackageSanityTests()
| Method Detail |
|---|
protected final void publicApiOnly()
public void testSerializable()
throws Exception
Serializable classes in the package. For a serializable
Class C:
C explicitly implements Object.equals(java.lang.Object), the deserialized instance will be
checked to be equal to the instance before serialization.
C doesn't explicitly implement equals but instead inherits it from a
superclass, no equality check is done on the deserialized instance because it's not clear
whether the author intended for the class to be a value type.
AbstractPackageSanityTests doesn't know how to construct, the test will fail.
C,
C is skipped for serialization test, even if it implements Serializable.
C or C's subtype.
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.
Exception
public void testNulls()
throws Exception
NullPointerTester checks for all top-level public classes in the package. For
a class C
Nullable should throw NullPointerException.
AbstractPackageSanityTests doesn't know how to construct, the test will fail.
C,
instance methods are skipped for nulls test.
C or C's subtype.
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.
Exception
public void testEquals()
throws Exception
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:
List.add(E),
or functional update methods such as Joiner.skipNulls().
AbstractPackageSanityTests doesn't know how to construct, the test will fail.
C,
C is skipped for equality test.
C or C's subtype.
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.
Exception
protected final <T> void setDefault(Class<T> type,
T value)
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.
protected final void ignoreClasses(com.google.common.base.Predicate<? super Class<?>> condition)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||