|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.testing.ClassSanityTester
@Beta public final class ClassSanityTester
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.
| 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. |
|
|
setDefault(Class<T> type,
T value)
Sets the default value for type. |
|
|
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 |
|---|
public ClassSanityTester()
| Method Detail |
|---|
public <T> ClassSanityTester setDefault(Class<T> type,
T value)
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 extends T>) instead.
public <T> ClassSanityTester setSampleInstances(Class<T> type,
Iterable<? extends T> instances)
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.
public void testNulls(Class<?> cls)
cls properly checks null on all constructor and method parameters that
aren't annotated with Nullable. In details:
Nullable should throw NullPointerException.
cls, all non-private instance methods will be checked too using the instance
created by invoking the constructor or static factory method.
cls:
cls, instance methods are skipped for nulls test.
cls or cls's subtype.
public void testEquals(Class<?> cls)
Object.equals(java.lang.Object) and Object.hashCode() of cls. In details:
cls, no test is performed.
cls or cls's subtype.
List.add(E),
or functional update methods such as Joiner.skipNulls().
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.
public ClassSanityTester.FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls)
cls, excluding superclasses.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||