Package org.assertj.core.internal
Class Objects
java.lang.Object
org.assertj.core.internal.Objects
Reusable assertions for
Object
s.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ComparisonStrategy
(package private) Failures
private static final GroupTypeDescription
private final FieldSupport
private static final Objects
(package private) final PropertySupport
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
actualIsExactlyInstanceOfType
(Object actual, Class<?> expectedType, AssertionInfo info) private boolean
Compares actual and other with standard strategy (null safe equals check).boolean
areEqualToComparingOnlyGivenFields
(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) boolean
areEqualToIgnoringGivenFields
(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) void
assertDoesNotHaveSameClassAs
(AssertionInfo info, Object actual, Object other) Verifies that the actual value does not have the same class as the given object.<A> void
assertDoesNotHaveSameHashCodeAs
(AssertionInfo info, A actual, Object other) Asserts that the actual object does not the same hashCode as the given object.void
assertDoesNotHaveToString
(AssertionInfo info, Object actual, String otherToString) Verifies that the actual ToString value is not equal to the given String.void
assertEqual
(AssertionInfo info, Object actual, Object expected) Asserts that two objects are equal.<A> void
assertHasAllNullFieldsOrPropertiesExcept
(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore) Asserts that the given object has null fields except the given ones.<A> void
assertHasFieldOrProperty
(AssertionInfo info, A actual, String name) <A> void
assertHasFieldOrPropertyWithValue
(AssertionInfo info, A actual, String name, Object expectedValue) <A> void
assertHasNoNullFieldsOrPropertiesExcept
(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore) Assert that the given object has no null fields except the given ones.<A> void
assertHasOnlyFields
(AssertionInfo info, A actual, String... names) void
assertHasSameClassAs
(AssertionInfo info, Object actual, Object other) Verifies that the actual value has the same class as the given object.<A> void
assertHasSameHashCodeAs
(AssertionInfo info, A actual, Object other) Asserts that the actual object has the same hashCode as the given object.void
assertHasToString
(AssertionInfo info, Object actual, String expectedToString) Verifies that the actual ToString value is equal to the given String.<A> void
assertIsEqualToComparingFieldByFieldRecursively
(AssertionInfo info, Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) Assert that the given object is "deeply" equals to other by comparing all fields recursively.<A> void
assertIsEqualToComparingOnlyGivenFields
(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) Assert that the given object is lenient equals to other object by comparing given fields value only.<A> void
assertIsEqualToIgnoringGivenFields
(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields) unless given ignored ones.<A> void
assertIsEqualToIgnoringNullFields
(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) Assert that the given object is lenient equals by ignoring null fields value on other object (including inherited fields).void
assertIsExactlyInstanceOf
(AssertionInfo info, Object actual, Class<?> type) Verifies that the actual value is exactly an instance of given type.void
assertIsIn
(AssertionInfo info, Object actual, Iterable<?> values) Asserts that the given object is present in the given collection.void
assertIsIn
(AssertionInfo info, Object actual, Object[] values) Asserts that the given object is present in the given array.void
assertIsInstanceOf
(AssertionInfo info, Object actual, Class<?> type) Verifies that the given object is an instance of the given type.void
assertIsInstanceOfAny
(AssertionInfo info, Object actual, Class<?>[] types) Verifies that the given object is an instance of any of the given types.void
assertIsNotExactlyInstanceOf
(AssertionInfo info, Object actual, Class<?> type) Verifies that the actual value is not exactly an instance of given type.void
assertIsNotIn
(AssertionInfo info, Object actual, Iterable<?> values) Asserts that the given object is not present in the given collection.void
assertIsNotIn
(AssertionInfo info, Object actual, Object[] values) Asserts that the given object is not present in the given array.void
assertIsNotInstanceOf
(AssertionInfo info, Object actual, Class<?> type) Verifies that the given object is not an instance of the given type.void
assertIsNotInstanceOfAny
(AssertionInfo info, Object actual, Class<?>[] types) Verifies that the given object is not an instance of any of the given types.void
assertIsNotOfAnyClassIn
(AssertionInfo info, Object actual, Class<?>[] types) Verifies that the actual value type is not in given types.void
assertIsOfAnyClassIn
(AssertionInfo info, Object actual, Class<?>[] types) Verifies that the actual value type is in given types.void
assertNotEqual
(AssertionInfo info, Object actual, Object other) Asserts that two objects are not equal.void
assertNotNull
(AssertionInfo info, Object actual) Asserts that the given object is notnull
.void
assertNotNull
(AssertionInfo info, Object actual, String label) Asserts that the given object is notnull
.void
assertNotSame
(AssertionInfo info, Object actual, Object other) Asserts that two objects do not refer to the same object.void
assertNull
(AssertionInfo info, Object actual) Asserts that the given object isnull
.void
assertSame
(AssertionInfo info, Object actual, Object expected) Asserts that two objects refer to the same object.private <A> boolean
canReadFieldValue
(Field field, A actual) private void
checkArrayIsNotNull
(Object[] values) private void
checkIsNotNullAndIsNotEmpty
(Class<?>[] types) private void
checkNotNullIterable
(Iterable<?> values) private <A> Object
extractPropertyOrField
(A actual, String name) Comparator
<?> Returns the declared fields of a given class excluding any synthetic or static fields.getDeclaredFieldsIncludingInherited
(Class<?> clazz) Returns the declared fields of given class and its superclasses stopping at superclass injava.lang
package whose fields are not included.getFieldsNames
(Class<?> clazz) private <A> Object
getPropertyOrFieldValue
(A a, String fieldName) Get property value first and in case of error try field value.private boolean
haveSameClass
(Object actual, Object other, AssertionInfo info) static Objects
instance()
Returns the singleton instance of this class based onStandardComparisonStrategy
.private boolean
isActualIn
(Object actual, Iterable<?> values) private <A> Objects.ByFieldsComparison
isEqualToComparingOnlyGivenFields
(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] fields) private <A> Objects.ByFieldsComparison
isEqualToIgnoringGivenFields
(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] givenIgnoredFields) private boolean
isInstanceOfClass
(Object actual, Class<?> clazz, AssertionInfo info) private boolean
isItemInArray
(Object item, Object[] arrayOfValues) Returnstrue
if given item is in given array,false
otherwise.private boolean
isOfOneOfGivenTypes
(Object actual, Class<?>[] types, AssertionInfo info) private boolean
objectIsInstanceOfOneOfGivenClasses
(Object actual, Class<?>[] types, AssertionInfo info) (package private) static boolean
propertyOrFieldValuesAreEqual
(Object actualFieldValue, Object otherFieldValue, String fieldName, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
-
Field Details
-
INSTANCE
-
FIELDS_GROUP_DESCRIPTION
-
propertySupport
-
comparisonStrategy
-
failures
Failures failures -
fieldSupport
-
-
Constructor Details
-
Objects
Objects() -
Objects
-
-
Method Details
-
instance
Returns the singleton instance of this class based onStandardComparisonStrategy
.- Returns:
- the singleton instance of this class based on
StandardComparisonStrategy
.
-
getComparator
-
getComparisonStrategy
-
getFailures
-
assertIsInstanceOf
Verifies that the given object is an instance of the given type.- Parameters:
info
- contains information about the assertion.actual
- the given object.type
- the type to check the given object against.- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if the given object isnull
.AssertionError
- if the given object is not an instance of the given type.
-
assertIsInstanceOfAny
Verifies that the given object is an instance of any of the given types.- Parameters:
info
- contains information about the assertion.actual
- the given object.types
- the types to check the given object against.- Throws:
NullPointerException
- if the given array isnull
.IllegalArgumentException
- if the given array is empty.NullPointerException
- if the given array hasnull
elements.AssertionError
- if the given object isnull
.AssertionError
- if the given object is not an instance of any of the given types.
-
objectIsInstanceOfOneOfGivenClasses
private boolean objectIsInstanceOfOneOfGivenClasses(Object actual, Class<?>[] types, AssertionInfo info) -
assertIsNotInstanceOf
Verifies that the given object is not an instance of the given type.- Parameters:
info
- contains information about the assertion.actual
- the given object.type
- the type to check the given object against.- Throws:
NullPointerException
- if the given type isnull
.AssertionError
- if the given object isnull
.AssertionError
- if the given object is an instance of the given type.
-
isInstanceOfClass
-
assertIsNotInstanceOfAny
Verifies that the given object is not an instance of any of the given types.- Parameters:
info
- contains information about the assertion.actual
- the given object.types
- the types to check the given object against.- Throws:
NullPointerException
- if the given array isnull
.IllegalArgumentException
- if the given array is empty.NullPointerException
- if the given array hasnull
elements.AssertionError
- if the given object isnull
.AssertionError
- if the given object is an instance of any of the given types.
-
assertHasSameClassAs
Verifies that the actual value has the same class as the given object.- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to check type against.- Throws:
AssertionError
- if the actual has not the same type has the given object.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
haveSameClass
-
assertDoesNotHaveSameClassAs
Verifies that the actual value does not have the same class as the given object.- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to check type against.- Throws:
AssertionError
- if the actual has the same type has the given object.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
assertIsExactlyInstanceOf
Verifies that the actual value is exactly an instance of given type.- Parameters:
info
- contains information about the assertion.actual
- the given object.type
- the type to check the actual value against.- Throws:
AssertionError
- if the actual is not exactly an instance of given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
actualIsExactlyInstanceOfType
private boolean actualIsExactlyInstanceOfType(Object actual, Class<?> expectedType, AssertionInfo info) -
assertIsNotExactlyInstanceOf
Verifies that the actual value is not exactly an instance of given type.- Parameters:
info
- contains information about the assertion.actual
- the given object.type
- the type to check the actual value against.- Throws:
AssertionError
- if the actual is exactly an instance of given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given object is null.
-
assertIsOfAnyClassIn
Verifies that the actual value type is in given types.- Parameters:
info
- contains information about the assertion.actual
- the given object.types
- the types to check the actual value against.- Throws:
AssertionError
- if the actual value type is in given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given types is null.
-
isOfOneOfGivenTypes
-
assertIsNotOfAnyClassIn
Verifies that the actual value type is not in given types.- Parameters:
info
- contains information about the assertion.actual
- the given object.types
- the types to check the actual value against.- Throws:
AssertionError
- if the actual value type is in given type.NullPointerException
- if the actual value is null.NullPointerException
- if the given types is null.
-
checkIsNotNullAndIsNotEmpty
-
assertEqual
Asserts that two objects are equal.- Parameters:
info
- contains information about the assertion.actual
- the "actual" object.expected
- the "expected" object.- Throws:
AssertionError
- ifactual
is not equal toexpected
. This method will throw aorg.junit.ComparisonFailure
instead if JUnit is in the classpath and the given objects are not equal.
-
assertNotEqual
Asserts that two objects are not equal.- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.- Throws:
AssertionError
- ifactual
is equal toother
.
-
areEqual
Compares actual and other with standard strategy (null safe equals check).- Parameters:
actual
- the object to compare to otherother
- the object to compare to actual- Returns:
- true if actual and other are equal (null safe equals check), false otherwise.
-
assertNull
Asserts that the given object isnull
.- Parameters:
info
- contains information about the assertion.actual
- the given object.- Throws:
AssertionError
- if the given object is notnull
.
-
assertNotNull
Asserts that the given object is notnull
.- Parameters:
info
- contains information about the assertion.actual
- the given object.- Throws:
AssertionError
- if the given object isnull
.
-
assertNotNull
Asserts that the given object is notnull
.- Parameters:
info
- contains information about the assertion.actual
- the given object.label
- the label to represent actual in the error message- Throws:
AssertionError
- if the given object isnull
.
-
assertSame
Asserts that two objects refer to the same object.- Parameters:
info
- contains information about the assertion.actual
- the given object.expected
- the expected object.- Throws:
AssertionError
- if the given objects do not refer to the same object.
-
assertNotSame
Asserts that two objects do not refer to the same object.- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.- Throws:
AssertionError
- if the given objects refer to the same object.
-
assertHasToString
Verifies that the actual ToString value is equal to the given String.- Parameters:
info
- contains information about the assertion.actual
- the given object.expectedToString
- the string to compare against.- Throws:
AssertionError
- if the actual ToString value is not equal to the given String.NullPointerException
- if the actual value is null.
-
assertDoesNotHaveToString
Verifies that the actual ToString value is not equal to the given String.- Parameters:
info
- contains information about the assertion.actual
- the given object.otherToString
- the string to compare against.- Throws:
AssertionError
- if the actual ToString value is equal to the given String.NullPointerException
- if the actual value is null.
-
assertIsIn
Asserts that the given object is present in the given array.- Parameters:
info
- contains information about the assertion.actual
- the given object.values
- the given array.- Throws:
NullPointerException
- if the given array isnull
.AssertionError
- if the given object is not present in the given array.
-
assertIsNotIn
Asserts that the given object is not present in the given array.- Parameters:
info
- contains information about the assertion.actual
- the given object.values
- the given array.- Throws:
NullPointerException
- if the given array isnull
.AssertionError
- if the given object is present in the given array.
-
checkArrayIsNotNull
-
isItemInArray
Returnstrue
if given item is in given array,false
otherwise.- Parameters:
item
- the object to look for in arrayOfValuesarrayOfValues
- the array of values- Returns:
true
if given item is in given array,false
otherwise.
-
assertIsIn
Asserts that the given object is present in the given collection.- Parameters:
info
- contains information about the assertion.actual
- the given object.values
- the given iterable.- Throws:
NullPointerException
- if the given collection isnull
.AssertionError
- if the given object is not present in the given collection.
-
assertIsNotIn
Asserts that the given object is not present in the given collection.- Parameters:
info
- contains information about the assertion.actual
- the given object.values
- the given collection.- Throws:
NullPointerException
- if the given iterable isnull
.AssertionError
- if the given object is present in the given collection.
-
checkNotNullIterable
-
isActualIn
-
assertIsEqualToIgnoringNullFields
public <A> void assertIsEqualToIgnoringNullFields(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) Assert that the given object is lenient equals by ignoring null fields value on other object (including inherited fields).- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.comparatorByPropertyOrField
- comparators use for specific fieldscomparatorByType
- comparators use for specific types- Throws:
NullPointerException
- if the actual type isnull
.NullPointerException
- if the other type isnull
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.
-
assertIsEqualToComparingOnlyGivenFields
public <A> void assertIsEqualToComparingOnlyGivenFields(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) Assert that the given object is lenient equals to other object by comparing given fields value only.- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.comparatorByPropertyOrField
- comparators use for specific fieldscomparatorByType
- comparators use for specific typesfields
- accepted fields- Throws:
NullPointerException
- if the other type isnull
.AssertionError
- if actual isnull
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.IntrospectionError
- if a field does not exist in actual.
-
isEqualToComparingOnlyGivenFields
private <A> Objects.ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] fields) -
assertIsEqualToIgnoringGivenFields
public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields) unless given ignored ones.- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.comparatorByPropertyOrField
- comparators use for specific fieldscomparatorByType
- comparators use for specific typesfields
- the fields to ignore in comparison- Throws:
NullPointerException
- if the other type isnull
.AssertionError
- if actual isnull
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.
-
isEqualToIgnoringGivenFields
private <A> Objects.ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] givenIgnoredFields) -
propertyOrFieldValuesAreEqual
static boolean propertyOrFieldValuesAreEqual(Object actualFieldValue, Object otherFieldValue, String fieldName, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) -
canReadFieldValue
-
assertHasNoNullFieldsOrPropertiesExcept
public <A> void assertHasNoNullFieldsOrPropertiesExcept(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore) Assert that the given object has no null fields except the given ones.- Type Parameters:
A
- the actual type.- Parameters:
info
- contains information about the assertion.actual
- the given object.propertiesOrFieldsToIgnore
- the fields to ignore in comparison.- Throws:
AssertionError
- if actual isnull
.AssertionError
- if some of the fields of the actual object are null.
-
assertHasAllNullFieldsOrPropertiesExcept
public <A> void assertHasAllNullFieldsOrPropertiesExcept(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore) Asserts that the given object has null fields except the given ones.- Type Parameters:
A
- the actual type.- Parameters:
info
- contains information about the assertion.actual
- the given object.propertiesOrFieldsToIgnore
- the fields to ignore in comparison.- Throws:
AssertionError
- is actual isnull
.AssertionError
- if some of the fields of the actual object are not null.
-
assertIsEqualToComparingFieldByFieldRecursively
public <A> void assertIsEqualToComparingFieldByFieldRecursively(AssertionInfo info, Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) Assert that the given object is "deeply" equals to other by comparing all fields recursively.- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to compareactual
to.comparatorByPropertyOrField
- comparators use for specific fieldscomparatorByType
- comparators use for specific types- Throws:
AssertionError
- if actual isnull
.AssertionError
- if the actual and the given object are not "deeply" equal.
-
getPropertyOrFieldValue
Get property value first and in case of error try field value.This method supports nested field/property (e.g. "address.street.number").
- Type Parameters:
A
- the actual type- Parameters:
a
- the object to get field value fromfieldName
- Field name to read, can be nested- Returns:
- (nested) field value or property value if field was not accessible.
- Throws:
IntrospectionError
- is field value can't get retrieved.
-
getDeclaredFieldsIncludingInherited
Returns the declared fields of given class and its superclasses stopping at superclass injava.lang
package whose fields are not included.- Parameters:
clazz
- the class we want the declared fields.- Returns:
- the declared fields of given class and its superclasses.
-
getFieldsNames
-
getDeclaredFieldsIgnoringSyntheticAndStatic
Returns the declared fields of a given class excluding any synthetic or static fields.Synthetic fields are fields that are generated by the compiler for access purposes, or by instrumentation tools e.g. JaCoCo adds in a $jacocoData field and therefore should be ignored when comparing fields.
Static fields are used as constants, and are not associated with an object.
- Parameters:
clazz
- the class we want the declared fields.- Returns:
- the declared fields of given class excluding any synthetic fields.
-
areEqualToIgnoringGivenFields
public boolean areEqualToIgnoringGivenFields(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) -
areEqualToComparingOnlyGivenFields
public boolean areEqualToComparingOnlyGivenFields(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) -
assertHasFieldOrProperty
-
assertHasFieldOrPropertyWithValue
public <A> void assertHasFieldOrPropertyWithValue(AssertionInfo info, A actual, String name, Object expectedValue) -
assertHasOnlyFields
-
extractPropertyOrField
-
assertHasSameHashCodeAs
Asserts that the actual object has the same hashCode as the given object.- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to check hashCode against.- Throws:
AssertionError
- if the actual object is null.AssertionError
- if the given object is null.AssertionError
- if the actual object has not the same hashCode as the given object.
-
assertDoesNotHaveSameHashCodeAs
Asserts that the actual object does not the same hashCode as the given object.- Type Parameters:
A
- the actual type- Parameters:
info
- contains information about the assertion.actual
- the given object.other
- the object to check hashCode against.- Throws:
AssertionError
- if the actual object is null.AssertionError
- if the given object is null.AssertionError
- if the actual object has the same hashCode as the given object.
-