How to use ByFieldsComparison method of org.assertj.core.internal.Objects class

Best Assertj code snippet using org.assertj.core.internal.Objects.ByFieldsComparison

Source:Objects.java Github

copy

Full Screen

...508 * @throws IntrospectionError if a field does not exist in actual.509 */510 public <A> void assertIsEqualToComparingOnlyGivenFields(AssertionInfo info, A actual, A other, String... fields) {511 assertNotNull(info, actual);512 ByFieldsComparison byFieldsComparison = isEqualToComparingOnlyGivenFields(actual, other, fields);513 if (byFieldsComparison.isFieldsNamesNotEmpty())514 throw failures.failure(info, shouldBeEqualComparingOnlyGivenFields(actual, byFieldsComparison.fieldsNames,515 byFieldsComparison.rejectedValues,516 byFieldsComparison.expectedValues,517 newArrayList(fields)));518 }519 private <A> ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, String[] fields) {520 List<String> rejectedFieldsNames = new LinkedList<>();521 List<Object> expectedValues = new LinkedList<>();522 List<Object> rejectedValues = new LinkedList<>();523 for (String fieldName : fields) {524 Object actualFieldValue = getPropertyOrFieldValue(actual, fieldName);525 Object otherFieldValue = getPropertyOrFieldValue(other, fieldName);526 if (!org.assertj.core.util.Objects.areEqual(actualFieldValue, otherFieldValue)) {527 rejectedFieldsNames.add(fieldName);528 expectedValues.add(otherFieldValue);529 rejectedValues.add(actualFieldValue);530 }531 }532 return new ByFieldsComparison(rejectedFieldsNames, expectedValues, rejectedValues);533 }534 /**535 * Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields)536 * unless given ignored ones.537 *538 * @param info contains information about the assertion.539 * @param actual the given object.540 * @param other the object to compare {@code actual} to.541 * @param fields the fields to ignore in comparison542 * @throws NullPointerException if the other type is {@code null}.543 * @throws AssertionError if actual is {@code null}.544 * @throws AssertionError if the actual and the given object are not lenient equals.545 * @throws AssertionError if the other object is not an instance of the actual type.546 */547 public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, String... fields) {548 assertNotNull(info, actual);549 ByFieldsComparison byFieldsComparison = isEqualToIgnoringGivenFields(actual, other, fields);550 if (byFieldsComparison.isFieldsNamesNotEmpty())551 throw failures.failure(info, shouldBeEqualToIgnoringGivenFields(actual, byFieldsComparison.fieldsNames,552 byFieldsComparison.rejectedValues,553 byFieldsComparison.expectedValues,554 newArrayList(fields)));555 }556 private <A> ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, String[] givenIgnoredFields) {557 Set<Field> declaredFieldsIncludingInherited = getDeclaredFieldsIncludingInherited(actual.getClass());558 List<String> fieldsNames = new LinkedList<>();559 List<Object> expectedValues = new LinkedList<>();560 List<Object> rejectedValues = new LinkedList<>();561 Set<String> ignoredFields = newLinkedHashSet(givenIgnoredFields);562 for (Field field : declaredFieldsIncludingInherited) {563 // ignore private field if user has decided not to use them in comparison564 if (ignoredFields.contains(field.getName()) || !canReadFieldValue(field, actual)) {565 continue;566 }567 Object actualFieldValue = getPropertyOrFieldValue(actual, field.getName());568 Object otherFieldValue = getPropertyOrFieldValue(other, field.getName());569 if (!org.assertj.core.util.Objects.areEqual(actualFieldValue, otherFieldValue)) {570 fieldsNames.add(field.getName());571 rejectedValues.add(actualFieldValue);572 expectedValues.add(otherFieldValue);573 }574 }575 return new ByFieldsComparison(fieldsNames, expectedValues, rejectedValues);576 }577 private <A> boolean canReadFieldValue(Field field, A actual) {578 return fieldSupport.isAllowedToRead(field) || propertySupport.publicGetterExistsFor(field.getName(), actual);579 }580 /**581 * Get property value first and in case of error try field value.582 * <p>583 * This method supports nested field/property (e.g. "address.street.number").584 *585 * @param a the object to get field value from586 * @param fieldName Field name to read, can be nested587 * @return (nested) field value or property value if field was not accessible.588 * @throws IntrospectionError is field value can't get retrieved.589 */590 private <A> Object getPropertyOrFieldValue(A a, String fieldName) {591 return PropertyOrFieldSupport.COMPARISON.getValueOf(fieldName, a);592 }593 /**594 * Returns the declared fields of given class and its superclasses stopping at superclass in <code>java.lang</code>595 * package whose fields are not included.596 *597 * @param clazz the class we want the declared fields.598 * @return the declared fields of given class and its superclasses.599 */600 private static Set<Field> getDeclaredFieldsIncludingInherited(Class<?> clazz) {601 checkNotNull(clazz, "expecting Class parameter not to be null");602 Set<Field> declaredFields = newLinkedHashSet(clazz.getDeclaredFields());603 // get fields declared in superclass604 Class<?> superclazz = clazz.getSuperclass();605 while (superclazz != null && !superclazz.getName().startsWith("java.lang")) {606 declaredFields.addAll(newLinkedHashSet(superclazz.getDeclaredFields()));607 superclazz = superclazz.getSuperclass();608 }609 return declaredFields;610 }611 public boolean areEqualToIgnoringGivenFields(Object actual, Object other, String... fields) {612 return isEqualToIgnoringGivenFields(actual, other, fields).isFieldsNamesEmpty();613 }614 public boolean areEqualToComparingOnlyGivenFields(Object actual, Object other, String... fields) {615 return isEqualToComparingOnlyGivenFields(actual, other, fields).isFieldsNamesEmpty();616 }617 public <A> void assertHasFieldOrProperty(AssertionInfo info, A actual, String name) {618 assertNotNull(info, actual);619 try {620 extractPropertyOrField(actual, name);621 } catch (IntrospectionError error) {622 throw failures.failure(info, shouldHavePropertyOrField(actual, name));623 }624 }625 public <A> void assertHasFieldOrPropertyWithValue(AssertionInfo info, A actual, String name, Object expectedValue) {626 assertHasFieldOrProperty(info, actual, name);627 Object value = extractPropertyOrField(actual, name);628 if (!org.assertj.core.util.Objects.areEqual(value, expectedValue))629 throw failures.failure(info, shouldHavePropertyOrFieldWithValue(actual, name, expectedValue, value));630 }631 private <A> Object extractPropertyOrField(A actual, String name) {632 return PropertyOrFieldSupport.EXTRACTION.getValueOf(name, actual);633 }634 public static class ByFieldsComparison {635 private final List<String> fieldsNames;636 private final List<Object> expectedValues;637 private final List<Object> rejectedValues;638 public ByFieldsComparison(final List<String> fieldsNames,639 final List<Object> expectedValues,640 final List<Object> rejectedValues) {641 this.fieldsNames = fieldsNames;642 this.expectedValues = expectedValues;643 this.rejectedValues = rejectedValues;644 }645 public ByFieldsComparison() {646 this(new ArrayList<String>(), new ArrayList<>(), new ArrayList<>());647 }648 public boolean isFieldsNamesEmpty() {649 return fieldsNames.isEmpty();650 }651 public boolean isFieldsNamesNotEmpty() {652 return !isFieldsNamesEmpty();653 }654 }655}...

Full Screen

Full Screen

ByFieldsComparison

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.internal.Objects;3import org.junit.Test;4public class ObjectsByFieldsComparisonTest {5 public void test() {6 Objects objects = Objects.instance();7 Employee employee1 = new Employee("John", "Doe", 25);8 Employee employee2 = new Employee("John", "Doe", 25);9 objects.assertIsEqualToComparingFieldByFieldRecursively(Assertions.assertThat(employee1), employee2);10 }11 static class Employee {12 private String firstName;13 private String lastName;14 private int age;15 public Employee(String firstName, String lastName, int age) {16 this.firstName = firstName;17 this.lastName = lastName;18 this.age = age;19 }20 public String getFirstName() {21 return firstName;22 }23 public String getLastName() {24 return lastName;25 }26 public int getAge() {27 return age;28 }29 }30}31at org.assertj.core.internal.Objects.assertIsEqualToComparingFieldByFieldRecursively(Objects.java:206)

Full Screen

Full Screen

ByFieldsComparison

Using AI Code Generation

copy

Full Screen

1public void shouldCompareTwoObjectsByFields() {2 Person person1 = new Person("John", "Doe", 25);3 Person person2 = new Person("John", "Doe", 25);4 assertThat(person1).usingComparatorForFields((p1, p2) -> {5 assertThat(p1).hasNoNullFieldsOrProperties();6 assertThat(p2).hasNoNullFieldsOrProperties();7 return Integer.compare(p1.getAge(), p2.getAge());8 }, "age").isEqualTo(person2);9}10public class Person {11 private final String firstName;12 private final String lastName;13 private final int age;14 public Person(String firstName, String lastName, int age) {15 this.firstName = firstName;16 this.lastName = lastName;17 this.age = age;18 }19 public String getFirstName() {20 return firstName;21 }22 public String getLastName() {23 return lastName;24 }25 public int getAge() {26 return age;27 }28}29public class Person {30 private final String firstName;31 private final String lastName;32 private final int age;33 public Person(String firstName, String lastName, int age) {34 this.firstName = firstName;35 this.lastName = lastName;36 this.age = age;37 }38 public String getFirstName() {39 return firstName;40 }41 public String getLastName() {42 return lastName;43 }44 public int getAge() {45 return age;46 }47}

Full Screen

Full Screen

ByFieldsComparison

Using AI Code Generation

copy

Full Screen

1public void testByFieldsComparison() {2 Objects objects = new Objects();3 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();4}5public void testByFieldsComparison() {6 Objects objects = new Objects();7 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();8}9public void testByFieldsComparison() {10 Objects objects = new Objects();11 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();12}13public void testByFieldsComparison() {14 Objects objects = new Objects();15 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();16}17public void testByFieldsComparison() {18 Objects objects = new Objects();19 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();20}21public void testByFieldsComparison() {22 Objects objects = new Objects();23 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();24}25public void testByFieldsComparison() {26 Objects objects = new Objects();27 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();28}29public void testByFieldsComparison() {30 Objects objects = new Objects();31 assertThat(objects.areEqualByComparingFields(actual, expected)).isFalse();32}33public void testByFieldsComparison() {

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.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful