Package org.assertj.core.internal
Class Objects
- java.lang.Object
-
- org.assertj.core.internal.Objects
-
public class Objects extends Object
Reusable assertions forObjects.- Author:
- Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObjects.ByFieldsComparison
-
Constructor Summary
Constructors Constructor Description Objects(ComparisonStrategy comparisonStrategy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanareEqualToComparingOnlyGivenFields(Object actual, Object other, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields)booleanareEqualToIgnoringGivenFields(Object actual, Object other, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields)voidassertDoesNotHaveSameClassAs(AssertionInfo info, Object actual, Object other)Verifies that the actual value does not have the same class as the given object.voidassertEqual(AssertionInfo info, Object actual, Object expected)Asserts that two objects are equal.<A> voidassertHasFieldOrProperty(AssertionInfo info, A actual, String name)<A> voidassertHasFieldOrPropertyWithValue(AssertionInfo info, A actual, String name, Object expectedValue)<A> voidassertHasNoNullFieldsOrPropertiesExcept(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore)Assert that the given object has no null fields except the given ones.voidassertHasSameClassAs(AssertionInfo info, Object actual, Object other)Verifies that the actual value has the same class as the given object.voidassertHasToString(AssertionInfo info, Object actual, String expectedToString)<A> voidassertIsEqualToComparingFieldByFieldRecursively(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> voidassertIsEqualToComparingOnlyGivenFields(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> voidassertIsEqualToIgnoringGivenFields(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> voidassertIsEqualToIgnoringNullFields(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).voidassertIsExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type)Verifies that the actual value is exactly an instance of given type.voidassertIsIn(AssertionInfo info, Object actual, Iterable<?> values)Asserts that the given object is present in the given collection.voidassertIsIn(AssertionInfo info, Object actual, Object[] values)Asserts that the given object is present in the given array.voidassertIsInstanceOf(AssertionInfo info, Object actual, Class<?> type)Verifies that the given object is an instance of the given type.voidassertIsInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types)Verifies that the given object is an instance of any of the given types.voidassertIsNotExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type)Verifies that the actual value is not exactly an instance of given type.voidassertIsNotIn(AssertionInfo info, Object actual, Iterable<?> values)Asserts that the given object is not present in the given collection.voidassertIsNotIn(AssertionInfo info, Object actual, Object[] values)Asserts that the given object is not present in the given array.voidassertIsNotInstanceOf(AssertionInfo info, Object actual, Class<?> type)Verifies that the given object is not an instance of the given type.voidassertIsNotInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types)Verifies that the given object is not an instance of any of the given types.voidassertIsNotOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types)Verifies that the actual value type is not in given types.voidassertIsOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types)Verifies that the actual value type is in given types.voidassertNotEqual(AssertionInfo info, Object actual, Object other)Asserts that two objects are not equal.voidassertNotNull(AssertionInfo info, Object actual)Asserts that the given object is notnull.voidassertNotSame(AssertionInfo info, Object actual, Object other)Asserts that two objects do not refer to the same object.voidassertNull(AssertionInfo info, Object actual)Asserts that the given object isnull.voidassertSame(AssertionInfo info, Object actual, Object expected)Asserts that two objects refer to the same object.Comparator<?>getComparator()ComparisonStrategygetComparisonStrategy()static Set<Field>getDeclaredFieldsIncludingInherited(Class<?> clazz)Returns the declared fields of given class and its superclasses stopping at superclass injava.langpackage whose fields are not included.static Objectsinstance()Returns the singleton instance of this class based onStandardComparisonStrategy.
-
-
-
Constructor Detail
-
Objects
public Objects(ComparisonStrategy comparisonStrategy)
-
-
Method Detail
-
instance
public static Objects instance()
Returns the singleton instance of this class based onStandardComparisonStrategy.- Returns:
- the singleton instance of this class based on
StandardComparisonStrategy.
-
getComparator
public Comparator<?> getComparator()
-
getComparisonStrategy
public ComparisonStrategy getComparisonStrategy()
-
assertIsInstanceOf
public void assertIsInstanceOf(AssertionInfo info, Object actual, Class<?> type)
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
public void assertIsInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types)
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 hasnullelements.AssertionError- if the given object isnull.AssertionError- if the given object is not an instance of any of the given types.
-
assertIsNotInstanceOf
public void assertIsNotInstanceOf(AssertionInfo info, Object actual, Class<?> type)
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.
-
assertIsNotInstanceOfAny
public void assertIsNotInstanceOfAny(AssertionInfo info, Object actual, Class<?>[] types)
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 hasnullelements.AssertionError- if the given object isnull.AssertionError- if the given object is an instance of any of the given types.
-
assertHasSameClassAs
public void assertHasSameClassAs(AssertionInfo info, Object actual, Object other)
Verifies that the actual value has the same class as the given object.- Parameters:
info- contains information about the assertion.actual- the given object.- 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.
-
assertDoesNotHaveSameClassAs
public void assertDoesNotHaveSameClassAs(AssertionInfo info, Object actual, Object other)
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
public void assertIsExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type)
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.
-
assertIsNotExactlyInstanceOf
public void assertIsNotExactlyInstanceOf(AssertionInfo info, Object actual, Class<?> type)
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
public void assertIsOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types)
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.
-
assertIsNotOfAnyClassIn
public void assertIsNotOfAnyClassIn(AssertionInfo info, Object actual, Class<?>[] types)
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.
-
assertEqual
public void assertEqual(AssertionInfo info, Object actual, Object expected)
Asserts that two objects are equal.- Parameters:
info- contains information about the assertion.actual- the "actual" object.expected- the "expected" object.- Throws:
AssertionError- ifactualis not equal toexpected. This method will throw aorg.junit.ComparisonFailureinstead if JUnit is in the classpath and the given objects are not equal.
-
assertNotEqual
public void assertNotEqual(AssertionInfo info, Object actual, Object other)
Asserts that two objects are not equal.- Parameters:
info- contains information about the assertion.actual- the given object.other- the object to compareactualto.- Throws:
AssertionError- ifactualis equal toother.
-
assertNull
public void assertNull(AssertionInfo info, Object actual)
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
public void assertNotNull(AssertionInfo info, Object actual)
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.
-
assertSame
public void assertSame(AssertionInfo info, Object actual, Object expected)
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
public void assertNotSame(AssertionInfo info, Object actual, Object other)
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 compareactualto.- Throws:
AssertionError- if the given objects refer to the same object.
-
assertHasToString
public void assertHasToString(AssertionInfo info, Object actual, String expectedToString)
-
assertIsIn
public void assertIsIn(AssertionInfo info, Object actual, Object[] values)
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.IllegalArgumentException- if the given array is empty.AssertionError- if the given object is not present in the given array.
-
assertIsNotIn
public void assertIsNotIn(AssertionInfo info, Object actual, Object[] values)
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.IllegalArgumentException- if the given array is empty.AssertionError- if the given object is present in the given array.
-
assertIsIn
public void assertIsIn(AssertionInfo info, Object actual, Iterable<?> values)
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.IllegalArgumentException- if the given collection is empty.AssertionError- if the given object is not present in the given collection.
-
assertIsNotIn
public void assertIsNotIn(AssertionInfo info, Object actual, Iterable<?> values)
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.IllegalArgumentException- if the given collection is empty.AssertionError- if the given object is present in the given collection.
-
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).- Parameters:
info- contains information about the assertion.actual- the given object.other- the object to compareactualto.- 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.- Parameters:
info- contains information about the assertion.actual- the given object.other- the object to compareactualto.fields- 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.
-
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.- Parameters:
info- contains information about the assertion.actual- the given object.other- the object to compareactualto.fields- 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.
-
assertHasNoNullFieldsOrPropertiesExcept
public <A> void assertHasNoNullFieldsOrPropertiesExcept(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore)
Assert that the given object has no null fields except the given ones.- 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.
-
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.- Parameters:
info- contains information about the assertion.actual- the given object.other- the object to compareactualto.- Throws:
AssertionError- if actual isnull.AssertionError- if the actual and the given object are not "deeply" equal.
-
getDeclaredFieldsIncludingInherited
public static Set<Field> getDeclaredFieldsIncludingInherited(Class<?> clazz)
Returns the declared fields of given class and its superclasses stopping at superclass injava.langpackage whose fields are not included.- Parameters:
clazz- the class we want the declared fields.- Returns:
- the declared fields of given class and its superclasses.
-
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
public <A> void assertHasFieldOrProperty(AssertionInfo info, A actual, String name)
-
assertHasFieldOrPropertyWithValue
public <A> void assertHasFieldOrPropertyWithValue(AssertionInfo info, A actual, String name, Object expectedValue)
-
-