@Beta public final class ClassSanityTester extends Object
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.
Modifier and Type | Class and Description |
---|---|
class |
ClassSanityTester.FactoryMethodReturnValueTester
Runs sanity tests against return values of static factory methods declared by a class.
|
Constructor and Description |
---|
ClassSanityTester() |
Modifier and Type | Method and Description |
---|---|
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)
|
void |
testNulls(Class<?> cls)
Tests that
cls properly checks null on all constructor and method parameters that
aren't annotated with Nullable . |
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<T>, 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.Copyright © 2010-2014. All Rights Reserved.