Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.hasDualValuesToCompare
Source:RecursiveComparisonDifferenceCalculator.java
...78 public List<ComparisonDifference> getDifferences() {79 Collections.sort(differences);80 return differences;81 }82 public boolean hasDualValuesToCompare() {83 return !dualValuesToCompare.isEmpty();84 }85 public DualValue pickDualValueToCompare() {86 final DualValue dualValue = dualValuesToCompare.removeFirst();87 if (dualValue.hasPotentialCyclingValues()) {88 // visited dual values are here to avoid cycle, java types don't have cycle, there is no need to track them.89 // moreover this would make should_fix_1854_minimal_test to fail (see the test for a detailed explanation)90 visitedDualValues.add(dualValue);91 }92 return dualValue;93 }94 private void registerForComparison(DualValue dualValue) {95 if (!visitedDualValues.contains(dualValue)) dualValuesToCompare.addFirst(dualValue);96 }97 private void initDualValuesToCompare(Object actual, Object expected, List<String> parentPath, boolean isRootObject) {98 DualValue dualValue = new DualValue(parentPath, actual, expected);99 boolean mustCompareFieldsRecursively = mustCompareFieldsRecursively(isRootObject, dualValue);100 if (dualValue.hasNoNullValues() && dualValue.hasNoContainerValues() && mustCompareFieldsRecursively) {101 // disregard the equals method and start comparing fields102 Set<String> nonIgnoredActualFieldsNames = recursiveComparisonConfiguration.getNonIgnoredActualFieldNames(dualValue);103 if (!nonIgnoredActualFieldsNames.isEmpty()) {104 // fields to ignore are evaluated when adding their corresponding dualValues to dualValuesToCompare which filters105 // ignored fields according to recursiveComparisonConfiguration106 Set<String> expectedFieldsNames = getFieldsNames(expected.getClass());107 if (expectedFieldsNames.containsAll(nonIgnoredActualFieldsNames)) {108 // we compare actual fields vs expected, ignoring expected additional fields109 for (String nonIgnoredActualFieldName : nonIgnoredActualFieldsNames) {110 DualValue fieldDualValue = new DualValue(parentPath, nonIgnoredActualFieldName,111 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, actual),112 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, expected));113 dualValuesToCompare.addFirst(fieldDualValue);114 }115 } else {116 dualValuesToCompare.addFirst(dualValue);117 }118 } else {119 dualValuesToCompare.addFirst(dualValue);120 }121 } else {122 dualValuesToCompare.addFirst(dualValue);123 }124 // We need to remove already visited fields pair to avoid infinite recursion in case125 // parent -> set{child} with child having a reference back to parent126 // it occurs to unordered collection where we compare all possible combination of the collection elements recursively127 // --128 // remove visited values one by one, DualValue.equals correctly compare respective actual and expected fields by reference129 visitedDualValues.forEach(visitedDualValue -> dualValuesToCompare.stream()130 .filter(dualValueToCompare -> dualValueToCompare.equals(visitedDualValue))131 .findFirst()132 .ifPresent(dualValuesToCompare::remove));133 }134 private boolean mustCompareFieldsRecursively(boolean isRootObject, DualValue dualValue) {135 boolean noCustomComparisonForDualValue = !recursiveComparisonConfiguration.hasCustomComparator(dualValue)136 && !shouldHonorOverriddenEquals(dualValue, recursiveComparisonConfiguration);137 return isRootObject || noCustomComparisonForDualValue;138 }139 }140 /**141 * Compare two objects for differences by doing a 'deep' comparison. This will traverse the142 * Object graph and perform either a field-by-field comparison on each143 * object (if not .equals() method has been overridden from Object), or it144 * will call the customized .equals() method if it exists.145 * <p>146 *147 * This method handles cycles correctly, for example A->B->C->A.148 * Suppose a and a' are two separate instances of the A with the same values149 * for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It150 * uses cycle detection storing visited objects in a Set to prevent endless151 * loops.152 *153 * @param actual Object one to compare154 * @param expected Object two to compare155 * @param recursiveComparisonConfiguration the recursive comparison configuration156 * @return the list of differences found or an empty list if objects are equivalent.157 * Equivalent means that all field values of both subgraphs are the same,158 * either at the field level or via the respectively encountered overridden159 * .equals() methods during traversal.160 */161 public List<ComparisonDifference> determineDifferences(Object actual, Object expected,162 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {163 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(actual, expected)) {164 return list(expectedAndActualTypeDifference(actual, expected));165 }166 List<String> rootPath = list();167 List<DualValue> visited = list();168 return determineDifferences(actual, expected, rootPath, true, visited, recursiveComparisonConfiguration);169 }170 // TODO keep track of ignored fields in an RecursiveComparisonExecution class ?171 private static List<ComparisonDifference> determineDifferences(Object actual, Object expected, List<String> parentPath,172 boolean isRootObject, List<DualValue> visited,173 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {174 ComparisonState comparisonState = new ComparisonState(visited, recursiveComparisonConfiguration);175 comparisonState.initDualValuesToCompare(actual, expected, parentPath, isRootObject);176 while (comparisonState.hasDualValuesToCompare()) {177 final DualValue dualValue = comparisonState.pickDualValueToCompare();178 final List<String> currentPath = dualValue.getPath();179 final Object actualFieldValue = dualValue.actual;180 final Object expectedFieldValue = dualValue.expected;181 if (actualFieldValue == expectedFieldValue) continue;182 // Custom comparators take precedence over all other types of comparison183 if (recursiveComparisonConfiguration.hasCustomComparator(dualValue)) {184 if (!propertyOrFieldValuesAreEqual(dualValue, recursiveComparisonConfiguration)) comparisonState.addDifference(dualValue);185 // since we used a custom comparator we don't need to inspect the nested fields any further186 continue;187 }188 if (actualFieldValue == null || expectedFieldValue == null) {189 // one of the value is null while the other is not as we already know that actualFieldValue != expectedFieldValue190 comparisonState.addDifference(dualValue);...
hasDualValuesToCompare
Using AI Code Generation
1public class RecursiveComparisonDifferenceCalculatorTest {2 private static final String EXPECTED = "expected";3 private static final String ACTUAL = "actual";4 private static final String DESCRIPTION = "description";5 public void should_return_false_when_both_values_are_null() {6 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator(null, null, null);7 assertThat(calculator.hasDualValuesToCompare()).isFalse();8 }9 public void should_return_true_when_both_values_are_not_null() {10 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator(EXPECTED, ACTUAL, null);11 assertThat(calculator.hasDualValuesToCompare()).isTrue();12 }13 public void should_return_true_when_only_expected_value_is_null() {14 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator(null, ACTUAL, null);15 assertThat(calculator.hasDualValuesToCompare()).isTrue();16 }17 public void should_return_true_when_only_actual_value_is_null() {18 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator(EXPECTED, null, null);19 assertThat(calculator.hasDualValuesToCompare()).isTrue();20 }21}
hasDualValuesToCompare
Using AI Code Generation
1public RecursiveComparisonDifferenceCalculatorTest()2public void should_return_false_when_differences_is_empty()3public void should_return_false_when_differences_is_not_empty()4public void should_return_false_when_differences_has_only_dual_values()5public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_collection()6public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_map()7public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_an_array()8public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_primitive_array()9public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_primitive_array()10public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_object_array()11public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_primitive_array_of_arrays()12public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_object_array_of_arrays()13public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_array_of_multi_dimensional_array_of_primitive()14public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_array_of_multi_dimensional_array_of_object()15public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_array_of_multi_dimensional_array_of_multi_dimensional_array_of_primitive()16public void should_return_true_when_differences_has_only_dual_values_and_one_of_them_is_a_multi_dimensional_array_of_multi_dimensional_array_of_multi_dimensional_array_of_object()
hasDualValuesToCompare
Using AI Code Generation
1 public void test() {2 final MyObject expected = new MyObject("a", "b");3 final MyObject actual = new MyObject("a", "c");4 final RecursiveComparisonAssert<MyObject> comparison = assertThat(actual).usingRecursiveComparison();5 comparison.isEqualTo(expected);6 }7 private static class MyObject {8 private final String a;9 private final String b;10 public MyObject(String a, String b) {11 this.a = a;12 this.b = b;13 }14 public String getA() {15 return a;16 }17 public String getB() {18 return b;19 }20 }21 <MyObject{a=a, b=c}>22 <MyObject{a=a, b=b}>23assertThat(new File("actual.txt")).hasSameContentAs(new File("expected.txt"));
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!!