How to use WithArray method of org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_with_arrays_Test class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_with_arrays_Test.WithArray

Source:RecursiveComparisonAssert_isEqualTo_with_arrays_Test.java Github

copy

Full Screen

...25 @ParameterizedTest(name = "author 1 {0} / author 2 {1}")26 @MethodSource("sameArrays")27 void should_pass_when_comparing_same_array_fields(Author[] authors1, Author[] authors2) {28 // GIVEN29 WithArray<Author> actual = new WithArray<>(authors1);30 WithArray<Author> expected = new WithArray<>(authors2);31 // THEN32 assertThat(actual).usingRecursiveComparison()33 .isEqualTo(expected);34 }35 static Stream<Arguments> sameArrays() {36 Author pratchett = new Author("Terry Pratchett");37 Author georgeMartin = new Author("George Martin");38 Author none = null;39 Author[] empty = array();40 return Stream.of(Arguments.of(array(pratchett), array(pratchett)),41 Arguments.of(array(pratchett, georgeMartin), array(pratchett, georgeMartin)),42 Arguments.of(array(pratchett, none), array(pratchett, none)),43 Arguments.of(empty, empty));44 }45 @ParameterizedTest(name = "authors 1 {0} / authors 2 {1} / path {2} / value 1 {3}/ value 2 {4}")46 @MethodSource("differentArrays")47 void should_fail_when_comparing_different_array_fields(Author[] authors1, Author[] authors2,48 String path, Object value1, Object value2, String desc) {49 // GIVEN50 WithArray<Author> actual = new WithArray<>(authors1);51 WithArray<Author> expected = new WithArray<>(authors2);52 // WHEN53 compareRecursivelyFailsAsExpected(actual, expected);54 // THEN55 ComparisonDifference difference = desc == null ? diff(path, value1, value2) : diff(path, value1, value2, desc);56 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);57 }58 static Stream<Arguments> differentArrays() {59 Author pratchett = new Author("Terry Pratchett");60 Author georgeMartin = new Author("George Martin");61 Author none = null;62 return Stream.of(Arguments.of(array(pratchett), array(georgeMartin), "group.name", "Terry Pratchett", "George Martin", null),63 Arguments.of(array(pratchett, georgeMartin), array(pratchett), "group",64 array(pratchett, georgeMartin), array(pratchett),65 "actual and expected values are arrays of different size, actual size=2 when expected size=1"),66 Arguments.of(array(pratchett), array(none), "group", pratchett, null, null),67 Arguments.of(array(none), array(pratchett), "group", null, pratchett, null));68 }69 @ParameterizedTest(name = "authors {0} / object {1} / path {2} / value 1 {3}/ value 2 {4}")70 @MethodSource("arrayWithNonArrays")71 void should_fail_when_comparing_array_to_non_array(Object actualFieldValue, Author[] expectedFieldValue,72 String path, Object value1, Object value2, String desc) {73 // GIVEN74 WithObject actual = new WithObject(actualFieldValue);75 WithArray<Author> expected = new WithArray<>(expectedFieldValue);76 // WHEN77 compareRecursivelyFailsAsExpected(actual, expected);78 // THEN79 ComparisonDifference difference = desc == null ? diff(path, value1, value2) : diff(path, value1, value2, desc);80 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);81 }82 static Stream<Arguments> arrayWithNonArrays() {83 Author pratchett = new Author("Terry Pratchett");84 Author georgeMartin = new Author("George Martin");85 // we need to use the actual array and the expected list otherwise verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall86 // fails as actualArray and expectedList description includes their instance reference (@123ff3f) to differentiate their87 // otherwise similar description88 Author[] expectedFieldValue = array(pratchett, georgeMartin);89 List<Author> actualFieldValue = list(pratchett, georgeMartin);90 return Stream.of(Arguments.of(pratchett, array(pratchett), "group", pratchett, array(pratchett),91 "expected field is an array but actual field is not (org.assertj.core.api.recursive.comparison.Author)"),92 Arguments.of(actualFieldValue, expectedFieldValue, "group", actualFieldValue, expectedFieldValue,93 "expected field is an array but actual field is not (java.util.ArrayList)"));94 }95 public static class WithArray<E> {96 public E[] group;97 @SafeVarargs98 public WithArray(E... items) {99 this.group = items;100 }101 @Override102 public String toString() {103 return format("WithArray group=%s", list(group));104 }105 }106}...

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.

Most used method in RecursiveComparisonAssert_isEqualTo_with_arrays_Test

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful