Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.pickDualValueToCompare
Source:RecursiveComparisonDifferenceCalculator.java
...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);191 continue;...
pickDualValueToCompare
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatThrownBy;3import static org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration.builder;4import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.pickDualValueToCompare;5import java.util.List;6import org.assertj.core.api.recursive.comparison.DualValue;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;8import org.assertj.core.util.introspection.IntrospectionError;9import org.junit.jupiter.api.Test;10class RecursiveComparisonDifferenceCalculatorTest {11 void should_pick_dual_value_to_compare() {12 RecursiveComparisonConfiguration recursiveComparisonConfiguration = builder().build();13 List<String> actual = List.of("a", "b");14 List<String> other = List.of("a", "c");15 DualValue dualValue = pickDualValueToCompare("name", actual, other, recursiveComparisonConfiguration);16 assertThat(dualValue).isEqualToComparingFieldByField(new DualValue(actual, other));17 }18 void should_throw_IntrospectionError_when_actual_is_null() {19 RecursiveComparisonConfiguration recursiveComparisonConfiguration = builder().build();20 List<String> actual = null;21 List<String> other = List.of("a", "c");22 assertThatThrownBy(() -> pickDualValueToCompare("name", actual, other, recursiveComparisonConfiguration))23 .isInstanceOf(IntrospectionError.class)24 .hasMessage("Can't read property \"name\" of null");25 }26 void should_throw_IntrospectionError_when_other_is_null() {27 RecursiveComparisonConfiguration recursiveComparisonConfiguration = builder().build();28 List<String> actual = List.of("a", "b");29 List<String> other = null;30 assertThatThrownBy(() -> pickDualValueToCompare("name", actual, other, recursiveComparisonConfiguration))31 .isInstanceOf(IntrospectionError.class)32 .hasMessage("Can't read property \"name\" of null");33 }34}
pickDualValueToCompare
Using AI Code Generation
1import static java.util.Collections.singletonList;2import static org.assertj.core.api.Assertions.assertThat;3import static org.assertj.core.api.Assertions.within;4import static org.assertj.core.data.Percentage.withPercentage;5import static org.assertj.core.util.Lists.newArrayList;6import static org.assertj.core.util.Lists.list;7import java.util.List;8import org.assertj.core.api.recursive.comparison.DualValue;9import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;10import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;11import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.DifferenceComparisonStrategy;12import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.DifferenceComparisonStrategyFactory;13import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.DifferenceComparisonStrategyFactory.DifferenceComparisonStrategyType;14import org.assertj.core.internal.DeepDifference.Difference;15import org.assertj.core.util.introspection.PropertyOrFieldSupport;16public class RecursiveComparisonDifferenceCalculatorTest {17 private static final DifferenceComparisonStrategyFactory DIFFERENCE_COMPARISON_STRATEGY_FACTORY = new DifferenceComparisonStrategyFactory();18 private static final PropertyOrFieldSupport PROPERTY_OR_FIELD_SUPPORT = new PropertyOrFieldSupport();19 public static void main(String[] args) {20 List<DualValue> dualValues = newArrayList(21 new DualValue("0", "0"),22 new DualValue("1", "1"),23 new DualValue("2", "2"),24 new DualValue("3", "3"),25 new DualValue("4", "4"),26 new DualValue("5", "5"),27 new DualValue("6", "6"),28 new DualValue("7", "7"),29 new DualValue("8", "8"),30 new DualValue("9", "9"),31 new DualValue("10", "10"),32 new DualValue("11", "11"),33 new DualValue("12", "12"),34 new DualValue("13", "13"),35 new DualValue("14", "14"),36 new DualValue("15", "15"),37 new DualValue("16", "16"),38 new DualValue("17", "17"),39 new DualValue("18", "18"),40 new DualValue("19", "19"),41 new DualValue("20", "20"),42 new DualValue("21", "21"),43 new DualValue("22", "22"),44 new DualValue("
pickDualValueToCompare
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.DualValue;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.DualValueToCompare;4public class RecursiveComparisonDifferenceCalculatorTest {5 public static void main(String[] args) {6 DualValue dualValue = new DualValue(1, 2);7 DualValueToCompare dualValueToCompare = RecursiveComparisonDifferenceCalculator.pickDualValueToCompare(dualValue);8 System.out.println(dualValueToCompare);9 }10}11import org.assertj.core.api.recursive.comparison.DualValue;12import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;13import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.DualValueToCompare;14public class RecursiveComparisonDifferenceCalculatorTest {15 public static void main(String[] args) {16 DualValue dualValue = new DualValue(1, 2);17 DualValueToCompare dualValueToCompare = RecursiveComparisonDifferenceCalculator.pickDualValueToCompare(dualValue);18 System.out.println(dualValueToCompare);19 }20}
pickDualValueToCompare
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;2import org.assertj.core.internal.objects.data.Person;3class PersonTest {4 void test() {5 Person actual = new Person("John", 30);6 Person other = new Person("John", 30);7 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();8 boolean result = calculator.pickDualValueToCompare(actual, other, "name", "age");9 System.out.println(result);10 }11}
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!!