Best Assertj code snippet using org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver.assertRecursively
Source:RecursiveAssertionDriver.java
...44 public RecursiveAssertionDriver(RecursiveAssertionConfiguration configuration) {45 this.configuration = configuration;46 }47 public List<FieldLocation> assertOverObjectGraph(Predicate<Object> predicate, Object graphNode) {48 assertRecursively(predicate, graphNode, graphNode.getClass(), rootFieldLocation());49 return fieldsFailingTheAssertion.stream().sorted().collect(toList());50 }51 public void reset() {52 visitedNodeIds.clear();53 fieldsFailingTheAssertion.clear();54 }55 private void assertRecursively(Predicate<Object> predicate, Object node, Class<?> nodeType, FieldLocation fieldLocation) {56 if (nodeMustBeIgnored(node, nodeType, fieldLocation)) return;57 boolean nodeAlreadyVisited = markNodeAsVisited(node);58 if (nodeAlreadyVisited) return;59 if (!isRootObject(fieldLocation) && shouldEvaluateAssertion(nodeType)) {60 evaluateAssertion(predicate, node, fieldLocation);61 }62 recurseIntoFieldsOfCurrentNode(predicate, node, nodeType, fieldLocation);63 }64 private boolean nodeMustBeIgnored(Object node, Class<?> nodeType, FieldLocation fieldLocation) {65 return isNullWhichAreIgnored(node)66 || isPrimitiveWhichAreIgnored(nodeType)67 || configuration.matchesAnIgnoredField(fieldLocation)68 || configuration.matchesAnIgnoredFieldRegex(fieldLocation)69 || configuration.getIgnoredTypes().contains(nodeType);70 }71 private boolean isRootObject(FieldLocation fieldLocation) {72 return fieldLocation.equals(rootFieldLocation());73 }74 private boolean isNullWhichAreIgnored(Object node) {75 return node == null && configuration.shouldIgnoreAllNullFields();76 }77 private boolean isPrimitiveWhichAreIgnored(Class<?> nodeType) {78 return configuration.shouldIgnorePrimitiveFields() && isPrimitiveOrWrapper(nodeType);79 }80 private void evaluateAssertion(Predicate<Object> predicate, Object node, FieldLocation fieldLocation) {81 if (assertionFails(predicate, node)) {82 fieldsFailingTheAssertion.add(fieldLocation);83 }84 }85 private boolean assertionFails(Predicate<Object> predicate, Object node) {86 return !predicate.test(node);87 }88 private boolean shouldEvaluateAssertion(Class<?> nodeType) {89 boolean ignoreContainerAssertion = configuration.shouldIgnoreContainer() && isContainer(nodeType);90 boolean ignoreMapAssertion = configuration.shouldIgnoreMap() && isMap(nodeType);91 boolean ignoreOptionalAssertion = configuration.shouldIgnoreOptional() && isOptionalOrPrimitiveOptional(nodeType);92 return !(ignoreContainerAssertion || ignoreMapAssertion || ignoreOptionalAssertion);93 }94 private boolean isContainer(Class<?> nodeType) {95 return isCollection(nodeType) || isArray(nodeType);96 }97 private void recurseIntoFieldsOfCurrentNode(Predicate<Object> predicate, Object node, Class<?> nodeType,98 FieldLocation fieldLocation) {99 if (isTypeRequiringSpecificHandling(nodeType)) {100 if (shouldRecurseOverSpecialTypes(nodeType)) {101 doRecursionForSpecialTypes(predicate, node, nodeType, fieldLocation);102 }103 } else if (shouldRecurseIntoNode(node)) {104 evaluateFieldsOfCurrentNodeRecursively(predicate, node, fieldLocation);105 }106 }107 private boolean isTypeRequiringSpecificHandling(Class<?> nodeType) {108 return isCollection(nodeType) || isMap(nodeType) || isArray(nodeType) || isOptionalOrPrimitiveOptional(nodeType);109 }110 private boolean shouldRecurseOverSpecialTypes(Class<?> nodeType) {111 boolean recurseOverContainer = isContainer(nodeType)112 && configuration.getCollectionAssertionPolicy() != COLLECTION_OBJECT_ONLY;113 boolean recurseOverMap = isMap(nodeType) && configuration.getMapAssertionPolicy() != MAP_OBJECT_ONLY;114 boolean recurseOverOptional = isOptionalOrPrimitiveOptional(nodeType)115 && configuration.getOptionalAssertionPolicy() != OPTIONAL_OBJECT_ONLY;116 return recurseOverContainer || recurseOverMap || recurseOverOptional;117 }118 private void doRecursionForSpecialTypes(Predicate<Object> predicate, Object node, Class<?> nodeType,119 FieldLocation fieldLocation) {120 if (isCollection(nodeType)) {121 recurseIntoCollection(predicate, (Collection<?>) node, fieldLocation);122 } else if (isArray(nodeType)) {123 recurseIntoArray(predicate, node, nodeType, fieldLocation);124 } else if (isMap(nodeType)) {125 recurseIntoMap(predicate, (Map<?, ?>) node, fieldLocation);126 } else if (isOptionalOrPrimitiveOptional(nodeType)) {127 recurseIntoOptional(predicate, node, fieldLocation);128 }129 }130 private void recurseIntoCollection(Predicate<Object> predicate, Collection<?> collection, FieldLocation fieldLocation) {131 // TODO handle collection if needed by policy132 int index = 0;133 for (Object element : collection) {134 assertRecursively(predicate, element, safeGetClass(element), fieldLocation.field(format(INDEX_FORMAT, index)));135 index++;136 }137 }138 private void recurseIntoArray(Predicate<Object> predicate, Object node, Class<?> nodeType, FieldLocation fieldLocation) {139 Class<?> arrayType = nodeType.getComponentType();140 Object[] array = Arrays.asObjectArray(node);141 for (int i = 0; i < array.length; i++) {142 assertRecursively(predicate, array[i], arrayType, fieldLocation.field(format(INDEX_FORMAT, i)));143 }144 }145 private void recurseIntoOptional(Predicate<Object> predicate, Object node, FieldLocation fieldLocation) {146 // If we are here, we know the node is an optional or a primitive optional147 if (node instanceof Optional) {148 Optional<?> optionalNode = (Optional<?>) node;149 if (optionalNode.isPresent()) {150 Class<?> nextNodeType = safeGetClass(optionalNode.get());151 assertRecursively(predicate, optionalNode.get(), nextNodeType, fieldLocation.field("value"));152 }153 } else if (node instanceof OptionalInt) {154 OptionalInt optionalIntNode = (OptionalInt) node;155 if (optionalIntNode.isPresent()) {156 evaluateAssertion(predicate, optionalIntNode.getAsInt(), fieldLocation.field("value"));157 }158 } else if (node instanceof OptionalLong) {159 OptionalLong optionalLongNode = (OptionalLong) node;160 if (optionalLongNode.isPresent()) {161 evaluateAssertion(predicate, optionalLongNode.getAsLong(), fieldLocation.field("value"));162 }163 } else if (node instanceof OptionalDouble) {164 OptionalDouble optionalDoubleNode = (OptionalDouble) node;165 if (optionalDoubleNode.isPresent()) {166 evaluateAssertion(predicate, optionalDoubleNode.getAsDouble(), fieldLocation.field("value"));167 }168 }169 }170 private void recurseIntoMap(Predicate<Object> predicate, Map<?, ?> node, FieldLocation fieldLocation) {171 // If we are here, we can assume the policy is not MAP_OBJECT_ONLY172 // For both policies VALUES_ONLY and MAP_OBJECT_AND_ENTRIES we have to recurse over the values.173 recurseIntoMapValues(predicate, node, fieldLocation);174 if (configuration.getMapAssertionPolicy() == MAP_OBJECT_AND_ENTRIES) {175 recurseIntoMapKeys(predicate, node, fieldLocation);176 }177 }178 private void recurseIntoMapValues(Predicate<Object> predicate, Map<?, ?> currentNode, FieldLocation fieldLocation) {179 currentNode.values().forEach(nextNode -> recurseIntoMapElement(predicate, fieldLocation, nextNode, VALUE_FORMAT));180 }181 private void recurseIntoMapKeys(Predicate<Object> predicate, Map<?, ?> currentNode, FieldLocation fieldLocation) {182 currentNode.keySet().forEach(nextNode -> recurseIntoMapElement(predicate, fieldLocation, nextNode, KEY_FORMAT));183 }184 private void recurseIntoMapElement(Predicate<Object> predicate, FieldLocation fieldLocation, Object nextNode,185 String msgFormat) {186 Class<?> nextNodeType = safeGetClass(nextNode);187 String nextNodeFieldName = nextNode != null ? nextNode.toString() : NULL;188 assertRecursively(predicate, nextNode, nextNodeType, fieldLocation.field(format(msgFormat, nextNodeFieldName)));189 }190 private static Class<?> safeGetClass(Object object) {191 return object != null ? object.getClass() : Object.class;192 }193 private boolean shouldRecurseIntoNode(Object node) {194 return node != null && !nodeIsJavaTypeToIgnore(node);195 }196 private boolean nodeIsJavaTypeToIgnore(Object node) {197 String canonicalName = node.getClass().getCanonicalName();198 boolean isJCLType = canonicalName.startsWith("java.") || canonicalName.startsWith("javax.");199 return isJCLType && configuration.shouldSkipJavaLibraryTypeObjects();200 }201 private void evaluateFieldsOfCurrentNodeRecursively(Predicate<Object> predicate, Object node, FieldLocation fieldLocation) {202 configuration.getIntrospectionStrategy().getChildNodesOf(node).forEach(field -> {203 assertRecursively(predicate, field.value, field.type, fieldLocation.field(field.name));204 });205 }206 private boolean markNodeAsVisited(Object node) {207 // Cannot mark null nodes, so just lie and say marking succeeded...208 if (node == null) return false;209 String objectId = identityToString(node);210 return !visitedNodeIds.add(objectId);211 }212 /*213 * This is taken verbatim from org.apache.commons.lang3.ObjectUtils .214 */215 private static String identityToString(final Object object) {216 if (object == null) {217 return null;...
assertRecursively
Using AI Code Generation
1import static org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver.assertRecursively;2import java.util.List;3import org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver;4import org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver.RecursiveAssertionDriverBuilder;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;7public class RecursiveComparisonAssert_examples {8 public static void main(String[] args) {9 Person person = new Person("John", 30, new Address("Paris", "12th Street"));10 Person person2 = new Person("John", 30, new Address("Paris", "12th Street"));11 RecursiveComparisonConfiguration config = RecursiveComparisonConfiguration.builder()12 .withIgnoredFields("age")13 .build();14 RecursiveAssertionDriverBuilder<Person> builder = assertRecursively(person, config);15 List<RecursiveComparisonDifference> differences = builder.isEqualTo(person2);16 System.out.println(differences);17 }18 static class Person {19 String name;20 int age;21 Address address;22 public Person(String name, int age, Address address) {23 this.name = name;24 this.age = age;25 this.address = address;26 }27 public String getName() {28 return name;29 }30 public int getAge() {31 return age;32 }33 public Address getAddress() {34 return address;35 }36 }37 static class Address {38 String city;39 String street;40 public Address(String city, String street) {41 this.city = city;42 this.street = street;43 }44 public String getCity() {45 return city;46 }47 public String getStreet() {48 return street;49 }50 }51}52public static RecursiveAssertionDriverBuilder<T> assertRecursively(T actual, RecursiveComparisonConfiguration recursiveComparisonConfiguration)53public RecursiveAssertionDriverBuilder<T> isEqualTo(T expected)
assertRecursively
Using AI Code Generation
1import org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver;2import static org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver.assertRecursively;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert;4import static org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.assertRecursively;5assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonConfiguration recursiveComparisonConfiguration)6assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonAssert<?> expected)7assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonAssert<?> expected, RecursiveComparisonAssert<?> otherExpected)8assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonAssert<?> expected)9assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonAssert<?> expected, RecursiveComparisonAssert<?> otherExpected)10assertRecursively(RecursiveComparisonAssert<?> actual)11assertRecursively(RecursiveComparisonAssert<?> actual, RecursiveComparisonAssert<?> expected, RecursiveComparisonAssert<?> otherExpected, RecursiveComparisonAssert<?>... moreExpected)
assertRecursively
Using AI Code Generation
1import org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver;2import org.assertj.core.api.recursive.assertion.RecursiveComparisonConfiguration;3public class RecursiveAssertionDriverTest {4 public static void main(String[] args) {5 RecursiveAssertionDriver recursiveAssertionDriver = new RecursiveAssertionDriver();6 recursiveAssertionDriver.setRecursiveComparisonConfiguration(RecursiveComparisonConfiguration.builder().build());7 recursiveAssertionDriver.assertRecursively(new Person("foo", 42), new Person("foo", 42));8 }9}10public class Person {11 private String name;12 private int age;13 public Person(String name, int age) {14 this.name = name;15 this.age = age;16 }17 public String getName() {18 return name;19 }20 public void setName(String name) {21 this.name = name;22 }23 public int getAge() {24 return age;25 }26 public void setAge(int age) {27 this.age = age;28 }29}30public class Person {31 private String name;32 private int age;33 public Person(String name, int age) {34 this.name = name;35 this.age = age;36 }37 public String getName() {38 return name;39 }40 public void setName(String name) {41 this.name = name;42 }43 public int getAge() {44 return age;45 }46 public void setAge(int age) {47 this.age = age;48 }49}50public class Person {51 private String name;52 private int age;53 public Person(String name, int age) {54 this.name = name;55 this.age = age;56 }57 public String getName() {58 return name;59 }60 public void setName(String name) {61 this.name = name;62 }63 public int getAge() {64 return age;65 }66 public void setAge(int age) {67 this.age = age;68 }69}
assertRecursively
Using AI Code Generation
1public void testAssertRecursively() {2 Address address = new Address("street", "city", "state", "zip");3 User user = new User("name", address);4 assertThat(user).assertRecursively(new RecursiveAssertionDriver() {5 public void assertRecursiveComparison(RecursiveComparisonAssert<?> recursiveComparisonAssert) {6 .ignoringFields("name")7 .ignoringFields("address.city")8 .ignoringFields("address.state")9 .ignoringFields("address.zip");10 }11 });12}
assertRecursively
Using AI Code Generation
1import org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver;2RecursiveAssertionDriver assertionDriver = new RecursiveAssertionDriver();3Person person = new Person();4person.setName("John");5person.setAge(25);6person.setAddress(new Address("Street 1", "City 1", "Country 1"));7Person person2 = new Person();8person2.setName("John");9person2.setAge(25);10person2.setAddress(new Address("Street 1", "City 1", "Country 1"));11assertionDriver.assertRecursively(person, person2);12Person person3 = new Person();13person3.setName("John");14person3.setAge(25);15person3.setAddress(new Address("Street 2", "City 1", "Country 1"));16assertionDriver.assertRecursively(person, person3);17Person person4 = new Person();18person4.setName("John");19person4.setAge(26);20person4.setAddress(new Address("Street 1", "City 1", "Country 1"));21assertionDriver.assertRecursively(person, person4);22Person person5 = new Person();23person5.setName("John");24person5.setAge(25);25person5.setAddress(new Address("Street 1", "City 2", "Country 1"));26assertionDriver.assertRecursively(person, person5);27Person person6 = new Person();28person6.setName("John");29person6.setAge(25);30person6.setAddress(new Address("Street 1", "City 1", "Country 2"));31assertionDriver.assertRecursively(person, person6);32Person person7 = new Person();33person7.setName("John");34person7.setAge(25);35person7.setAddress(new Address("Street 1", "City 1", "Country 1"));36person7.setPhoneNumbers(Arrays.asList("1234567890", "0987654321"));
assertRecursively
Using AI Code Generation
1public void testAssertRecursively() {2 Employee employee = new Employee();3 employee.setName("John");4 employee.setAge(30);5 employee.setSalary(1000.00);6 employee.setAddress("London");7 assertThat(employee).assertRecursively(new RecursiveAssertionDriver() {8 public void recursiveAssert(Object actual) {9 assertThat(actual).isInstanceOf(Employee.class);10 Employee emp = (Employee) actual;11 assertThat(emp.getName()).isEqualTo("John");12 assertThat(emp.getAge()).isEqualTo(30);13 assertThat(emp.getSalary()).isEqualTo(1000.00);14 assertThat(emp.getAddress()).isEqualTo("London");15 }16 });17}18 at org.junit.Assert.assertEquals(Assert.java:115)19 at org.junit.Assert.assertEquals(Assert.java:144)20 at org.assertj.core.api.recursive.assertion.RecursiveAssertionDriverTest.lambda$testAssertRecursively$0(RecursiveAssertionDriverTest.java:30)21 at org.assertj.core.api.recursive.assertion.RecursiveAssertionDriver.assertRecursive(RecursiveAssertionDriver.java:34)22 at org.assertj.core.api.recursive.assertion.RecursiveAssertionDriverTest.testAssertRecursively(RecursiveAssertionDriverTest.java:29)23public class Employee {24 private String name;25 private int age;26 private double salary;27 private String address;28 private List<Employee> employees;29}30public class EmployeeTest {31 public void testAssertRecursively() {32 Employee employee = new Employee();33 employee.setName("John");
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!!