Class Objects


  • public class Objects
    extends java.lang.Object
    Reusable assertions for Objects.
    • Constructor Detail

      • Objects

        Objects()
    • Method Detail

      • getComparator

        public java.util.Comparator<?> getComparator()
      • getFailures

        public Failures getFailures()
      • assertIsInstanceOf

        public void assertIsInstanceOf​(AssertionInfo info,
                                       java.lang.Object actual,
                                       java.lang.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:
        java.lang.NullPointerException - if the given type is null.
        java.lang.AssertionError - if the given object is null.
        java.lang.AssertionError - if the given object is not an instance of the given type.
      • assertIsInstanceOfAny

        public void assertIsInstanceOfAny​(AssertionInfo info,
                                          java.lang.Object actual,
                                          java.lang.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:
        java.lang.NullPointerException - if the given array is null.
        java.lang.IllegalArgumentException - if the given array is empty.
        java.lang.NullPointerException - if the given array has null elements.
        java.lang.AssertionError - if the given object is null.
        java.lang.AssertionError - if the given object is not an instance of any of the given types.
      • objectIsInstanceOfOneOfGivenClasses

        private boolean objectIsInstanceOfOneOfGivenClasses​(java.lang.Object actual,
                                                            java.lang.Class<?>[] types,
                                                            AssertionInfo info)
      • assertIsNotInstanceOf

        public void assertIsNotInstanceOf​(AssertionInfo info,
                                          java.lang.Object actual,
                                          java.lang.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:
        java.lang.NullPointerException - if the given type is null.
        java.lang.AssertionError - if the given object is null.
        java.lang.AssertionError - if the given object is an instance of the given type.
      • isInstanceOfClass

        private boolean isInstanceOfClass​(java.lang.Object actual,
                                          java.lang.Class<?> clazz,
                                          AssertionInfo info)
      • assertIsNotInstanceOfAny

        public void assertIsNotInstanceOfAny​(AssertionInfo info,
                                             java.lang.Object actual,
                                             java.lang.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:
        java.lang.NullPointerException - if the given array is null.
        java.lang.IllegalArgumentException - if the given array is empty.
        java.lang.NullPointerException - if the given array has null elements.
        java.lang.AssertionError - if the given object is null.
        java.lang.AssertionError - if the given object is an instance of any of the given types.
      • assertHasSameClassAs

        public void assertHasSameClassAs​(AssertionInfo info,
                                         java.lang.Object actual,
                                         java.lang.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.
        other - the object to check type against.
        Throws:
        java.lang.AssertionError - if the actual has not the same type has the given object.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given object is null.
      • haveSameClass

        private boolean haveSameClass​(java.lang.Object actual,
                                      java.lang.Object other,
                                      AssertionInfo info)
      • assertDoesNotHaveSameClassAs

        public void assertDoesNotHaveSameClassAs​(AssertionInfo info,
                                                 java.lang.Object actual,
                                                 java.lang.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:
        java.lang.AssertionError - if the actual has the same type has the given object.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given object is null.
      • assertIsExactlyInstanceOf

        public void assertIsExactlyInstanceOf​(AssertionInfo info,
                                              java.lang.Object actual,
                                              java.lang.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:
        java.lang.AssertionError - if the actual is not exactly an instance of given type.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given object is null.
      • actualIsExactlyInstanceOfType

        private boolean actualIsExactlyInstanceOfType​(java.lang.Object actual,
                                                      java.lang.Class<?> expectedType,
                                                      AssertionInfo info)
      • assertIsNotExactlyInstanceOf

        public void assertIsNotExactlyInstanceOf​(AssertionInfo info,
                                                 java.lang.Object actual,
                                                 java.lang.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:
        java.lang.AssertionError - if the actual is exactly an instance of given type.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given object is null.
      • assertIsOfAnyClassIn

        public void assertIsOfAnyClassIn​(AssertionInfo info,
                                         java.lang.Object actual,
                                         java.lang.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:
        java.lang.AssertionError - if the actual value type is in given type.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given types is null.
      • isOfOneOfGivenTypes

        private boolean isOfOneOfGivenTypes​(java.lang.Object actual,
                                            java.lang.Class<?>[] types,
                                            AssertionInfo info)
      • assertIsNotOfAnyClassIn

        public void assertIsNotOfAnyClassIn​(AssertionInfo info,
                                            java.lang.Object actual,
                                            java.lang.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:
        java.lang.AssertionError - if the actual value type is in given type.
        java.lang.NullPointerException - if the actual value is null.
        java.lang.NullPointerException - if the given types is null.
      • checkIsNotNullAndIsNotEmpty

        private void checkIsNotNullAndIsNotEmpty​(java.lang.Class<?>[] types)
      • assertEqual

        public void assertEqual​(AssertionInfo info,
                                java.lang.Object actual,
                                java.lang.Object expected)
        Asserts that two objects are equal.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" object.
        expected - the "expected" object.
        Throws:
        java.lang.AssertionError - if actual is not equal to expected. This method will throw a org.junit.ComparisonFailure instead if JUnit is in the classpath and the given objects are not equal.
      • assertNotEqual

        public void assertNotEqual​(AssertionInfo info,
                                   java.lang.Object actual,
                                   java.lang.Object other)
        Asserts that two objects are not equal.
        Parameters:
        info - contains information about the assertion.
        actual - the given object.
        other - the object to compare actual to.
        Throws:
        java.lang.AssertionError - if actual is equal to other.
      • areEqual

        private boolean areEqual​(java.lang.Object actual,
                                 java.lang.Object other)
        Compares actual and other with standard strategy (null safe equals check).
        Parameters:
        actual - the object to compare to other
        other - the object to compare to actual
        Returns:
        true if actual and other are equal (null safe equals check), false otherwise.
      • assertNull

        public void assertNull​(AssertionInfo info,
                               java.lang.Object actual)
        Asserts that the given object is null.
        Parameters:
        info - contains information about the assertion.
        actual - the given object.
        Throws:
        java.lang.AssertionError - if the given object is not null.
      • assertNotNull

        public void assertNotNull​(AssertionInfo info,
                                  java.lang.Object actual)
        Asserts that the given object is not null.
        Parameters:
        info - contains information about the assertion.
        actual - the given object.
        Throws:
        java.lang.AssertionError - if the given object is null.
      • assertNotNull

        public void assertNotNull​(AssertionInfo info,
                                  java.lang.Object actual,
                                  java.lang.String label)
        Asserts that the given object is not null.
        Parameters:
        info - contains information about the assertion.
        actual - the given object.
        label - the label to represent actual in the error message
        Throws:
        java.lang.AssertionError - if the given object is null.
      • assertSame

        public void assertSame​(AssertionInfo info,
                               java.lang.Object actual,
                               java.lang.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:
        java.lang.AssertionError - if the given objects do not refer to the same object.
      • assertNotSame

        public void assertNotSame​(AssertionInfo info,
                                  java.lang.Object actual,
                                  java.lang.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 compare actual to.
        Throws:
        java.lang.AssertionError - if the given objects refer to the same object.
      • assertHasToString

        public void assertHasToString​(AssertionInfo info,
                                      java.lang.Object actual,
                                      java.lang.String expectedToString)
      • assertIsIn

        public void assertIsIn​(AssertionInfo info,
                               java.lang.Object actual,
                               java.lang.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:
        java.lang.NullPointerException - if the given array is null.
        java.lang.AssertionError - if the given object is not present in the given array.
      • assertIsNotIn

        public void assertIsNotIn​(AssertionInfo info,
                                  java.lang.Object actual,
                                  java.lang.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:
        java.lang.NullPointerException - if the given array is null.
        java.lang.AssertionError - if the given object is present in the given array.
      • checkArrayIsNotNull

        private void checkArrayIsNotNull​(java.lang.Object[] values)
      • isItemInArray

        private boolean isItemInArray​(java.lang.Object item,
                                      java.lang.Object[] arrayOfValues)
        Returns true if given item is in given array, false otherwise.
        Parameters:
        item - the object to look for in arrayOfValues
        arrayOfValues - the array of values
        Returns:
        true if given item is in given array, false otherwise.
      • assertIsIn

        public void assertIsIn​(AssertionInfo info,
                               java.lang.Object actual,
                               java.lang.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:
        java.lang.NullPointerException - if the given collection is null.
        java.lang.AssertionError - if the given object is not present in the given collection.
      • assertIsNotIn

        public void assertIsNotIn​(AssertionInfo info,
                                  java.lang.Object actual,
                                  java.lang.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:
        java.lang.NullPointerException - if the given iterable is null.
        java.lang.AssertionError - if the given object is present in the given collection.
      • checkNotNullIterable

        private void checkNotNullIterable​(java.lang.Iterable<?> values)
      • isActualIn

        private boolean isActualIn​(java.lang.Object actual,
                                   java.lang.Iterable<?> values)
      • assertIsEqualToIgnoringNullFields

        public <A> void assertIsEqualToIgnoringNullFields​(AssertionInfo info,
                                                          A actual,
                                                          A other,
                                                          java.util.Map<java.lang.String,​java.util.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 compare actual to.
        comparatorByPropertyOrField - comparators use for specific fields
        comparatorByType - comparators use for specific types
        Throws:
        java.lang.NullPointerException - if the actual type is null.
        java.lang.NullPointerException - if the other type is null.
        java.lang.AssertionError - if the actual and the given object are not lenient equals.
        java.lang.AssertionError - if the other object is not an instance of the actual type.
      • assertIsEqualToComparingOnlyGivenFields

        public <A> void assertIsEqualToComparingOnlyGivenFields​(AssertionInfo info,
                                                                A actual,
                                                                A other,
                                                                java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                TypeComparators comparatorByType,
                                                                java.lang.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 compare actual to.
        comparatorByPropertyOrField - comparators use for specific fields
        comparatorByType - comparators use for specific types
        fields - accepted fields
        Throws:
        java.lang.NullPointerException - if the other type is null.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual and the given object are not lenient equals.
        java.lang.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,
                                                                                 java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                                 TypeComparators comparatorByType,
                                                                                 java.lang.String[] fields)
      • assertIsEqualToIgnoringGivenFields

        public <A> void assertIsEqualToIgnoringGivenFields​(AssertionInfo info,
                                                           A actual,
                                                           A other,
                                                           java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                           TypeComparators comparatorByType,
                                                           java.lang.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 compare actual to.
        comparatorByPropertyOrField - comparators use for specific fields
        comparatorByType - comparators use for specific types
        fields - the fields to ignore in comparison
        Throws:
        java.lang.NullPointerException - if the other type is null.
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual and the given object are not lenient equals.
        java.lang.AssertionError - if the other object is not an instance of the actual type.
      • isEqualToIgnoringGivenFields

        private <A> Objects.ByFieldsComparison isEqualToIgnoringGivenFields​(A actual,
                                                                            A other,
                                                                            java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                            TypeComparators comparatorByType,
                                                                            java.lang.String[] givenIgnoredFields)
      • propertyOrFieldValuesAreEqual

        static boolean propertyOrFieldValuesAreEqual​(java.lang.Object actualFieldValue,
                                                     java.lang.Object otherFieldValue,
                                                     java.lang.String fieldName,
                                                     java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                     TypeComparators comparatorByType)
      • canReadFieldValue

        private <A> boolean canReadFieldValue​(java.lang.reflect.Field field,
                                              A actual)
      • assertHasNoNullFieldsOrPropertiesExcept

        public <A> void assertHasNoNullFieldsOrPropertiesExcept​(AssertionInfo info,
                                                                A actual,
                                                                java.lang.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:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if some of the fields of the actual object are null.
      • assertHasAllNullFieldsOrPropertiesExcept

        public <A> void assertHasAllNullFieldsOrPropertiesExcept​(AssertionInfo info,
                                                                 A actual,
                                                                 java.lang.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:
        java.lang.AssertionError - is actual is null.
        java.lang.AssertionError - if some of the fields of the actual object are not null.
      • assertIsEqualToComparingFieldByFieldRecursively

        public <A> void assertIsEqualToComparingFieldByFieldRecursively​(AssertionInfo info,
                                                                        java.lang.Object actual,
                                                                        java.lang.Object other,
                                                                        java.util.Map<java.lang.String,​java.util.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.
        comparatorByPropertyOrField - comparators use for specific fields
        comparatorByType - comparators use for specific types
        other - the object to compare actual to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual and the given object are not "deeply" equal.
      • getPropertyOrFieldValue

        private <A> java.lang.Object getPropertyOrFieldValue​(A a,
                                                             java.lang.String fieldName)
        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 from
        fieldName - 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

        public static java.util.Set<java.lang.reflect.Field> getDeclaredFieldsIncludingInherited​(java.lang.Class<?> clazz)
        Returns the declared fields of given class and its superclasses stopping at superclass in java.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

        public static java.util.Set<java.lang.String> getFieldsNames​(java.lang.Class<?> clazz)
      • getDeclaredFieldsIgnoringSyntheticAndStatic

        private static java.util.Set<java.lang.reflect.Field> getDeclaredFieldsIgnoringSyntheticAndStatic​(java.lang.Class<?> clazz)
        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​(java.lang.Object actual,
                                                     java.lang.Object other,
                                                     java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                     TypeComparators comparatorByType,
                                                     java.lang.String... fields)
      • areEqualToComparingOnlyGivenFields

        public boolean areEqualToComparingOnlyGivenFields​(java.lang.Object actual,
                                                          java.lang.Object other,
                                                          java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                          TypeComparators comparatorByType,
                                                          java.lang.String... fields)
      • assertHasFieldOrProperty

        public <A> void assertHasFieldOrProperty​(AssertionInfo info,
                                                 A actual,
                                                 java.lang.String name)
      • assertHasFieldOrPropertyWithValue

        public <A> void assertHasFieldOrPropertyWithValue​(AssertionInfo info,
                                                          A actual,
                                                          java.lang.String name,
                                                          java.lang.Object expectedValue)
      • extractPropertyOrField

        private <A> java.lang.Object extractPropertyOrField​(A actual,
                                                            java.lang.String name)
      • assertHasSameHashCodeAs

        public <A> void assertHasSameHashCodeAs​(AssertionInfo info,
                                                A actual,
                                                java.lang.Object other)
        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:
        java.lang.AssertionError - if the actual object is null.
        java.lang.AssertionError - if the given object is null.
        java.lang.AssertionError - if the actual object has not the same hashCode as the given object.