How to use getCustomErrorMessage method of org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.getCustomErrorMessage

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...73 this.dualValuesToCompare = new DualValueDeque(recursiveComparisonConfiguration);74 this.recursiveComparisonConfiguration = recursiveComparisonConfiguration;75 }76 void addDifference(DualValue dualValue) {77 differences.add(new ComparisonDifference(dualValue, null, getCustomErrorMessage(dualValue)));78 }79 void addDifference(DualValue dualValue, String description) {80 differences.add(new ComparisonDifference(dualValue, description, getCustomErrorMessage(dualValue)));81 }82 void addKeyDifference(DualValue parentDualValue, Object actualKey, Object expectedKey) {83 differences.add(new ComparisonKeyDifference(parentDualValue, actualKey, expectedKey));84 }85 public List<ComparisonDifference> getDifferences() {86 Collections.sort(differences);87 return differences;88 }89 public boolean hasDualValuesToCompare() {90 return !dualValuesToCompare.isEmpty();91 }92 public DualValue pickDualValueToCompare() {93 final DualValue dualValue = dualValuesToCompare.removeFirst();94 if (dualValue.hasPotentialCyclingValues()) {95 // visited dual values are here to avoid cycle, java types don't have cycle, there is no need to track them.96 // moreover this would make should_fix_1854_minimal_test to fail (see the test for a detailed explanation)97 visitedDualValues.add(dualValue);98 }99 return dualValue;100 }101 private void registerForComparison(DualValue dualValue) {102 if (!visitedDualValues.contains(dualValue)) dualValuesToCompare.addFirst(dualValue);103 }104 private void initDualValuesToCompare(Object actual, Object expected, FieldLocation fieldLocation) {105 DualValue dualValue = new DualValue(fieldLocation, actual, expected);106 boolean mustCompareFieldsRecursively = mustCompareFieldsRecursively(dualValue);107 if (dualValue.hasNoNullValues() && mustCompareFieldsRecursively) {108 // disregard the equals method and start comparing fields109 // TODO should fail if actual and expected don't have the same fields to compare (taking into account ignored/compared110 // fields)111 Set<String> actualFieldNamesToCompare = recursiveComparisonConfiguration.getActualFieldNamesToCompare(dualValue);112 if (!actualFieldNamesToCompare.isEmpty()) {113 // fields to ignore are evaluated when adding their corresponding dualValues to dualValuesToCompare which filters114 // ignored fields according to recursiveComparisonConfiguration115 Set<String> expectedFieldsNames = getFieldsNames(expected.getClass());116 if (expectedFieldsNames.containsAll(actualFieldNamesToCompare)) {117 // we compare actual fields vs expected, ignoring expected additional fields118 for (String nonIgnoredActualFieldName : actualFieldNamesToCompare) {119 DualValue fieldDualValue = new DualValue(fieldLocation.field(nonIgnoredActualFieldName),120 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, actual),121 COMPARISON.getSimpleValue(nonIgnoredActualFieldName, expected));122 dualValuesToCompare.addFirst(fieldDualValue);123 }124 } else {125 dualValuesToCompare.addFirst(dualValue);126 }127 } else {128 dualValuesToCompare.addFirst(dualValue);129 }130 } else {131 dualValuesToCompare.addFirst(dualValue);132 }133 // We need to remove already visited fields pair to avoid infinite recursion in case parent -> set{child} with child having134 // a reference back to its parent but only for complex types can have cycle, this is not the case for primitive or enums.135 // It occurs for unordered collection where we compare all possible combination of the collection elements recursively.136 // --137 // remove visited values one by one, DualValue.equals correctly compare respective actual and expected fields by reference138 visitedDualValues.forEach(visitedDualValue -> dualValuesToCompare.stream()139 .filter(dualValueToCompare -> dualValueToCompare.equals(visitedDualValue))140 .findFirst()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 the...

Full Screen

Full Screen

getCustomErrorMessage

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.within;3import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.getCustomErrorMessage;4import java.util.ArrayList;5import java.util.List;6import org.assertj.core.api.recursive.comparison.Difference;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;8import org.junit.Test;9public class AssertJRecursiveComparisonTest {10 public void testAssertJ() {11 List<String> list1 = new ArrayList<>();12 list1.add("a");13 list1.add("b");14 list1.add("c");15 List<String> list2 = new ArrayList<>();16 list2.add("a");17 list2.add("b");18 list2.add("d");19 List<Difference> differences = new RecursiveComparisonDifferenceCalculator().calculateDifferences(list1, list2);20 String message = getCustomErrorMessage(differences);21 System.out.println(message);22 }23}24when recursively comparing field by field, but found the following difference(s):25package com.baeldung.assertj;26import static org.assertj.core.api.Assertions.assertThat;27import static org.assertj.core.api.Assertions.within;28import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.getCustomErrorMessage;29import java.util.ArrayList;30import java.util.List;31import org.assertj.core.api.recursive.comparison.Difference;32import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;33import org.junit.Test;34public class AssertJRecursiveComparisonTest {35 public void testAssertJ() {36 List<String> list1 = new ArrayList<>();37 list1.add("a");38 list1.add("b");39 list1.add("

Full Screen

Full Screen

getCustomErrorMessage

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.within;3import java.util.ArrayList;4import java.util.List;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;6import org.junit.Test;7public class AssertJCustomErrorMessageTest {8 public void testCustomErrorMessage() {9 List<String> actual = new ArrayList<>();10 actual.add("A");11 actual.add("B");12 actual.add("C");13 actual.add("D");14 List<String> expected = new ArrayList<>();15 expected.add("A");16 expected.add("B");17 expected.add("C");18 expected.add("E");19 String customErrorMessage = RecursiveComparisonDifferenceCalculator.getCustomErrorMessage(actual, expected, within(1.0));20 System.out.println(customErrorMessage);21 }22}

Full Screen

Full Screen

getCustomErrorMessage

Using AI Code Generation

copy

Full Screen

1public class RecursiveComparisonDifferenceCalculator_getCustomErrorMessage_Test {2 public void should_get_custom_error_message() {3 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();4 String customErrorMessage = calculator.getCustomErrorMessage();5 assertThat(customErrorMessage).isEqualTo("custom error message");6 }7}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful