public final class ToStringVerifier
extends java.lang.Object
ToStringVerifier can be used in unit tests to verify that the Object.toString() returns
the desired results.
To get started, use ToStringVerifier as follows:
ToStringVerifier.forClass(MyClass.class).verify();
| Modifier and Type | Method and Description |
|---|---|
static ToStringVerifier |
forClass(java.lang.Class<?> clazz)
Create a verifier for the given class
|
static ToStringVerifier |
forClasses(java.lang.Class<?>... classes)
Create a verifier for the given classes
|
static ToStringVerifier |
forClasses(java.util.Collection<java.lang.Class<?>> classes)
Create a verifier for the given classes
|
static ToStringVerifier |
forPackage(java.lang.String packageName,
boolean scanRecursively)
Create a verifier for a given package
|
static ToStringVerifier |
forPackage(java.lang.String packageName,
boolean scanRecursively,
java.util.function.Predicate<java.lang.Class<?>> predicate)
Create a verifier for a given package
|
void |
verify()
Perform verification
|
ToStringVerifier |
withClassName(NameStyle nameStyle)
If specified, this tester will assert that the
Object.toString() output contains the name of the subject class in
the NameStyle specified. |
ToStringVerifier |
withFailOnExcludedFields(boolean failOnExcludedFields)
Define how the verifier should behave if the
Object.toString() output contains a field that has been explicitly excluded by
calling withIgnoredFields(String...) or implicitly excluded by calling withOnlyTheseFields(String...) or
withMatchingFields(String). |
<S> ToStringVerifier |
withFormatter(java.lang.Class<S> clazz,
Formatter<S> formatter)
Define a value formatter for the given class type.
|
ToStringVerifier |
withHashCode(boolean hashCode)
If set to true, this tester will assert that the
Object.toString() output contains the hash code as returned
by Object.hashCode(). |
ToStringVerifier |
withHashCodeProvider(HashCodeProvider hashCodeProvider)
With hash code provider
|
ToStringVerifier |
withIgnoredFields(java.util.Collection<java.lang.String> fields)
If specified, this tester will assert that all but the excluded fields are present in the
Object.toString() output. |
ToStringVerifier |
withIgnoredFields(java.lang.String... fields)
If specified, this tester will assert that all but the excluded fields are present in the
Object.toString() output. |
ToStringVerifier |
withInheritedFields(boolean inheritedFields)
This is enabled by default.
|
ToStringVerifier |
withMatchingFields(FieldFilter fields)
If specified, this tester will only assert that field matching this filter criteria are present in the
Object.toString() output. |
ToStringVerifier |
withMatchingFields(java.lang.String regex)
If specified, this tester will only assert that field names matching this regex pattern are present in the
Object.toString() output. |
ToStringVerifier |
withNullValue(java.lang.String nullValue)
The value to expect on the toString output when a field value is null.
|
ToStringVerifier |
withOnlyTheseFields(java.util.Collection<java.lang.String> fields)
If specified, this tester will only assert that the given fields are present in the
Object.toString() output. |
ToStringVerifier |
withOnlyTheseFields(java.lang.String... fields)
If specified, this tester will only assert that the given fields are present in the
Object.toString() output. |
<S> ToStringVerifier |
withPrefabValue(java.lang.Class<S> clazz,
S prefabValue)
Adds prefabricated values for instance fields of classes that ToStringVerifier cannot instantiate by itself.
|
ToStringVerifier |
withPreset(Preset preset)
A number of presets come with this library.
|
<S> ToStringVerifier |
withValueProvider(java.lang.Class<S> clazz,
java.util.function.Function<com.jparams.object.builder.path.Path,S> valueSupplier)
Adds value provider values for instance fields of classes that ToStringVerifier cannot instantiate by itself.
|
ToStringVerifier |
withValueProvider(com.jparams.object.builder.provider.Provider provider)
Adds value provider values for instance fields of classes that ToStringVerifier cannot instantiate by itself.
|
public static ToStringVerifier forClass(java.lang.Class<?> clazz)
clazz - class to testpublic static ToStringVerifier forClasses(java.lang.Class<?>... classes)
classes - classes to testpublic static ToStringVerifier forClasses(java.util.Collection<java.lang.Class<?>> classes)
classes - classes to testpublic static ToStringVerifier forPackage(java.lang.String packageName, boolean scanRecursively)
packageName - package under testscanRecursively - true to scan all sub-packagespublic static ToStringVerifier forPackage(java.lang.String packageName, boolean scanRecursively, java.util.function.Predicate<java.lang.Class<?>> predicate)
packageName - package under testscanRecursively - true to scan all sub-packagespredicate - defines which classes will be testedpublic ToStringVerifier withPreset(Preset preset)
Presets. A preset
is a pre-defined configuration for the ToStringVerifier.preset - presetpublic ToStringVerifier withClassName(NameStyle nameStyle)
Object.toString() output contains the name of the subject class in
the NameStyle specified.nameStyle - style of the class namepublic ToStringVerifier withHashCode(boolean hashCode)
Object.toString() output contains the hash code as returned
by Object.hashCode().hashCode - check for inclusion of the hash codepublic ToStringVerifier withInheritedFields(boolean inheritedFields)
inheritedFields - true to test for fields inherited from the parent classpublic ToStringVerifier withOnlyTheseFields(java.lang.String... fields)
Object.toString() output. To
assert all fields are present, do not call this method. To test for all fields excluding some, call withIgnoredFields(Collection).fields - field names to include in testpublic ToStringVerifier withOnlyTheseFields(java.util.Collection<java.lang.String> fields)
Object.toString() output. To
assert all fields are present, do not call this method. To test for all fields excluding some, call withIgnoredFields(Collection).fields - field names to include in testpublic ToStringVerifier withIgnoredFields(java.lang.String... fields)
Object.toString() output. To
assert only certain fields are present, do not call this method, instead use withOnlyTheseFields(Collection).fields - field names to exclude in testpublic ToStringVerifier withIgnoredFields(java.util.Collection<java.lang.String> fields)
Object.toString() output. To
assert only certain fields are present, do not call this method, instead use withOnlyTheseFields(Collection).fields - field names to exclude in testpublic ToStringVerifier withMatchingFields(java.lang.String regex)
Object.toString() output.regex - field name match regexpublic ToStringVerifier withMatchingFields(FieldFilter fields)
Object.toString() output.fields - filter criteriapublic ToStringVerifier withFailOnExcludedFields(boolean failOnExcludedFields)
Object.toString() output contains a field that has been explicitly excluded by
calling withIgnoredFields(String...) or implicitly excluded by calling withOnlyTheseFields(String...) or
withMatchingFields(String). By default, the verifier will not test for the presence of the excluded fields and will not fail
if they exist in the Object.toString() output when they should not. You can change this behaviour by setting failOnExcludedFields
to true.failOnExcludedFields - fail if ignored fields are found in the Object.toString() outputpublic <S> ToStringVerifier withPrefabValue(java.lang.Class<S> clazz, S prefabValue)
S - The class of the prefabricated values.clazz - The class of the prefabricated valuesprefabValue - An instance of S.public <S> ToStringVerifier withValueProvider(java.lang.Class<S> clazz, java.util.function.Function<com.jparams.object.builder.path.Path,S> valueSupplier)
S - The class of the prefabricated values.clazz - The class of the value to being suppliedvalueSupplier - value supplierpublic ToStringVerifier withValueProvider(com.jparams.object.builder.provider.Provider provider)
provider - Value providerpublic <S> ToStringVerifier withFormatter(java.lang.Class<S> clazz, Formatter<S> formatter)
S - value typeclazz - classformatter - formatterpublic ToStringVerifier withNullValue(java.lang.String nullValue)
nullnullValue - null valuepublic ToStringVerifier withHashCodeProvider(HashCodeProvider hashCodeProvider)
hashCodeProvider - hash code providerpublic void verify()
java.lang.AssertionError - if assertion conditions are not met