Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.shouldHonorEquals
Source:RecursiveComparisonDifferenceCalculator.java
...141 .ifPresent(dualValuesToCompare::remove));142 }143 private boolean mustCompareFieldsRecursively(DualValue dualValue) {144 return !recursiveComparisonConfiguration.hasCustomComparator(dualValue)145 && !shouldHonorEquals(dualValue, recursiveComparisonConfiguration)146 && dualValue.hasNoContainerValues();147 }148 private String getCustomErrorMessage(DualValue dualValue) {149 String fieldName = dualValue.getConcatenatedPath();150 // field custome messages take precedence over type messages151 if (recursiveComparisonConfiguration.hasCustomMessageForField(fieldName)) {152 return recursiveComparisonConfiguration.getMessageForField(fieldName);153 }154 Class<?> fieldType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();155 if (recursiveComparisonConfiguration.hasCustomMessageForType(fieldType)) {156 return recursiveComparisonConfiguration.getMessageForType(fieldType);157 }158 return null;159 }160 }161 /**162 * Compare two objects for differences by doing a 'deep' comparison. This will traverse the163 * Object graph and perform either a field-by-field comparison on each164 * object (if not .equals() method has been overridden from Object), or it165 * will call the customized .equals() method if it exists.166 * <p>167 *168 * This method handles cycles correctly, for example A->B->C->A.169 * Suppose a and a' are two separate instances of the A with the same values170 * for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It171 * uses cycle detection storing visited objects in a Set to prevent endless172 * loops.173 *174 * @param actual Object one to compare175 * @param expected Object two to compare176 * @param recursiveComparisonConfiguration the recursive comparison configuration177 * @return the list of differences found or an empty list if objects are equivalent.178 * Equivalent means that all field values of both subgraphs are the same,179 * either at the field level or via the respectively encountered overridden180 * .equals() methods during traversal.181 */182 public List<ComparisonDifference> determineDifferences(Object actual, Object expected,183 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {184 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(actual, expected)) {185 return list(expectedAndActualTypeDifference(actual, expected));186 }187 List<DualValue> visited = list();188 return determineDifferences(actual, expected, rootFieldLocation(), visited, recursiveComparisonConfiguration);189 }190 // TODO keep track of ignored fields in an RecursiveComparisonExecution class ?191 private static List<ComparisonDifference> determineDifferences(Object actual, Object expected, FieldLocation fieldLocation,192 List<DualValue> visited,193 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {194 ComparisonState comparisonState = new ComparisonState(visited, recursiveComparisonConfiguration);195 comparisonState.initDualValuesToCompare(actual, expected, fieldLocation);196 while (comparisonState.hasDualValuesToCompare()) {197 final DualValue dualValue = comparisonState.pickDualValueToCompare();198 final Object actualFieldValue = dualValue.actual;199 final Object expectedFieldValue = dualValue.expected;200 // Custom comparators take precedence over all other types of comparison201 if (recursiveComparisonConfiguration.hasCustomComparator(dualValue)) {202 if (!areDualValueEqual(dualValue, recursiveComparisonConfiguration)) comparisonState.addDifference(dualValue);203 // since we used a custom comparator we don't need to inspect the nested fields any further204 continue;205 }206 if (actualFieldValue == expectedFieldValue) continue;207 if (actualFieldValue == null || expectedFieldValue == null) {208 // one of the value is null while the other is not as we already know that actualFieldValue != expectedFieldValue209 comparisonState.addDifference(dualValue);210 continue;211 }212 if (dualValue.isExpectedAnEnum()) {213 compareAsEnums(dualValue, comparisonState, recursiveComparisonConfiguration);214 continue;215 }216 // TODO move hasFieldTypesDifference check into each compareXXX217 if (dualValue.isExpectedFieldAnArray()) {218 compareArrays(dualValue, comparisonState);219 continue;220 }221 // we compare ordered collections specifically as to be matching, each pair of elements at a given index must match.222 // concretely we compare: (col1[0] vs col2[0]), (col1[1] vs col2[1])...(col1[n] vs col2[n])223 if (dualValue.isExpectedFieldAnOrderedCollection()224 && !recursiveComparisonConfiguration.shouldIgnoreCollectionOrder(dualValue.fieldLocation)) {225 compareOrderedCollections(dualValue, comparisonState);226 continue;227 }228 if (dualValue.isExpectedFieldAnIterable()) {229 compareUnorderedIterables(dualValue, comparisonState);230 continue;231 }232 if (dualValue.isExpectedFieldAnOptional()) {233 compareOptional(dualValue, comparisonState);234 continue;235 }236 // Compare two SortedMaps taking advantage of the fact that these Maps can be compared in O(N) time due to their ordering237 if (dualValue.isExpectedFieldASortedMap()) {238 compareSortedMap(dualValue, comparisonState);239 continue;240 }241 // Compare two Unordered Maps. This is a slightly more expensive comparison because order cannot be assumed, therefore a242 // temporary Map must be created, however the comparison still runs in O(N) time.243 if (dualValue.isExpectedFieldAMap()) {244 compareUnorderedMap(dualValue, comparisonState);245 continue;246 }247 // compare Atomic types by value manually as they are container type and we can't use introspection in java 17+248 if (dualValue.isExpectedFieldAnAtomicBoolean()) {249 compareAtomicBoolean(dualValue, comparisonState);250 continue;251 }252 if (dualValue.isExpectedFieldAnAtomicInteger()) {253 compareAtomicInteger(dualValue, comparisonState);254 continue;255 }256 if (dualValue.isExpectedFieldAnAtomicIntegerArray()) {257 compareAtomicIntegerArray(dualValue, comparisonState);258 continue;259 }260 if (dualValue.isExpectedFieldAnAtomicLong()) {261 compareAtomicLong(dualValue, comparisonState);262 continue;263 }264 if (dualValue.isExpectedFieldAnAtomicLongArray()) {265 compareAtomicLongArray(dualValue, comparisonState);266 continue;267 }268 if (dualValue.isExpectedFieldAnAtomicReference()) {269 compareAtomicReference(dualValue, comparisonState);270 continue;271 }272 if (dualValue.isExpectedFieldAnAtomicReferenceArray()) {273 compareAtomicReferenceArray(dualValue, comparisonState);274 continue;275 }276 if (shouldHonorEquals(dualValue, recursiveComparisonConfiguration)) {277 if (!actualFieldValue.equals(expectedFieldValue)) comparisonState.addDifference(dualValue);278 continue;279 }280 Class<?> actualFieldValueClass = actualFieldValue.getClass();281 Class<?> expectedFieldClass = expectedFieldValue.getClass();282 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(dualValue)) {283 comparisonState.addDifference(dualValue,284 format(STRICT_TYPE_ERROR, expectedFieldClass.getName(), actualFieldValueClass.getName()));285 continue;286 }287 Set<String> actualNonIgnoredFieldsNames = recursiveComparisonConfiguration.getActualFieldNamesToCompare(dualValue);288 Set<String> expectedFieldsNames = getFieldsNames(expectedFieldClass);289 // Check if expected has more fields than actual, in that case the additional fields are reported as difference290 if (!expectedFieldsNames.containsAll(actualNonIgnoredFieldsNames)) {291 // report missing fields in actual292 Set<String> actualFieldsNamesNotInExpected = newHashSet(actualNonIgnoredFieldsNames);293 actualFieldsNamesNotInExpected.removeAll(expectedFieldsNames);294 String missingFields = actualFieldsNamesNotInExpected.toString();295 String expectedClassName = expectedFieldClass.getName();296 String actualClassName = actualFieldValueClass.getName();297 String missingFieldsDescription = format(MISSING_FIELDS, actualClassName, expectedClassName,298 expectedFieldClass.getSimpleName(), actualFieldValueClass.getSimpleName(),299 missingFields);300 comparisonState.addDifference(dualValue, missingFieldsDescription);301 } else { // TODO remove else to report more diff302 // compare actual's fields against expected :303 // - if actual has more fields than expected, the additional fields are ignored as expected is the reference304 for (String actualFieldName : actualNonIgnoredFieldsNames) {305 if (expectedFieldsNames.contains(actualFieldName)) {306 DualValue newDualValue = new DualValue(dualValue.fieldLocation.field(actualFieldName),307 COMPARISON.getSimpleValue(actualFieldName, actualFieldValue),308 COMPARISON.getSimpleValue(actualFieldName, expectedFieldValue));309 comparisonState.registerForComparison(newDualValue);310 }311 }312 }313 }314 return comparisonState.getDifferences();315 }316 // avoid comparing enum recursively since they contain static fields which are ignored in recursive comparison317 // this would make different field enum value to be considered the same!318 private static void compareAsEnums(final DualValue dualValue,319 ComparisonState comparisonState,320 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {321 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode()) {322 // we can use == for comparison which checks both actual and expected values and types are the same323 if (dualValue.actual != dualValue.expected) comparisonState.addDifference(dualValue);324 return;325 }326 if (!dualValue.isActualAnEnum()) {327 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an enum"));328 return;329 }330 // both actual and expected are enums331 Enum<?> actualEnum = (Enum<?>) dualValue.actual;332 Enum<?> expectedEnum = (Enum<?>) dualValue.expected;333 // we must only compare actual and expected enum by value but not by type334 if (!actualEnum.name().equals(expectedEnum.name())) comparisonState.addDifference(dualValue);335 }336 private static boolean shouldHonorEquals(DualValue dualValue,337 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {338 // since java 17 we can't introspect java types and get their fields so by default we compare them with equals339 // unless for some container like java types: iterables, array, optional, atomic values where we take the contained values340 // through accessors and register them in the recursive comparison.341 boolean shouldHonorJavaTypeEquals = dualValue.hasSomeJavaTypeValue() && !dualValue.isExpectedAContainer();342 return shouldHonorJavaTypeEquals || shouldHonorOverriddenEquals(dualValue, recursiveComparisonConfiguration);343 }344 private static boolean shouldHonorOverriddenEquals(DualValue dualValue,345 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {346 boolean shouldNotIgnoreOverriddenEqualsIfAny = !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue);347 return shouldNotIgnoreOverriddenEqualsIfAny && dualValue.actual != null && hasOverriddenEquals(dualValue.actual.getClass());348 }349 private static void compareArrays(DualValue dualValue, ComparisonState comparisonState) {350 if (!dualValue.isActualFieldAnArray()) {...
shouldHonorEquals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.shouldHonorEquals3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.shouldHonorEquals4RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)5RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)6RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)7RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)8RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)9RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)10RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)11RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)12RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)13RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)14RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)15RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)16RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)17RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)18RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)19RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)20RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)21RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)22RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)23RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)24RecursiveComparisonDifferenceCalculator.shouldHonorEquals(false)25RecursiveComparisonDifferenceCalculator.shouldHonorEquals(true)
shouldHonorEquals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.shouldHonorEquals;4RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();5recursiveComparisonConfiguration.shouldHonorEquals(shouldHonorEquals(false));6assertThat(actual).usingRecursiveComparison(recursiveComparisonConfiguration).isEqualTo(expected);
shouldHonorEquals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator2RecursiveComparisonDifferenceCalculator.shouldHonorEquals = { field ->3 type.isAssignableFrom(List) || type.isAssignableFrom(Set) || type.isAssignableFrom(Map) || type.isAssignableFrom(Collection)4}5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator6RecursiveComparisonDifferenceCalculator.shouldHonorEquals = { field ->7 type.isAssignableFrom(List) || type.isAssignableFrom(Set) || type.isAssignableFrom(Map) || type.isAssignableFrom(Collection) || type.isAssignableFrom(ConcurrentHashMap)8}9import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator10RecursiveComparisonDifferenceCalculator.shouldHonorEquals = { field ->11 type.isAssignableFrom(List) || type.isAssignableFrom(Set) || type.isAssignableFrom(Map) || type.isAssignableFrom(Collection) || type.isAssignableFrom(ConcurrentHashMap) || type.isAssignableFrom(ConcurrentSkipListMap)12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!