How to use Author class of org.assertj.core.api.recursive.comparison package

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

Source:RecursiveComparisonAssert_isEqualTo_Test.java Github

copy

Full Screen

1/*2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with3 * the License. You may obtain a copy of the License at4 *5 * http://www.apache.org/licenses/LICENSE-2.06 *7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the9 * specific language governing permissions and limitations under the License.10 *11 * Copyright 2012-2020 the original author or authors.12 */13package org.assertj.core.api.recursive.comparison;14import static org.assertj.core.api.Assertions.assertThat;15import static org.assertj.core.api.Assertions.entry;16import static org.assertj.core.api.BDDAssertions.then;17import static org.assertj.core.api.recursive.comparison.Color.BLUE;18import static org.assertj.core.api.recursive.comparison.Color.GREEN;19import static org.assertj.core.api.recursive.comparison.ColorWithCode.RED;20import static org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_Test.EmployeeDTO.JobTitle.QA_ENGINEER;21import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;22import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;23import static org.assertj.core.test.AlwaysEqualComparator.ALWAY_EQUALS_STRING;24import static org.assertj.core.util.Lists.list;25import static org.junit.jupiter.api.condition.OS.WINDOWS;26import static org.junit.jupiter.params.provider.Arguments.arguments;27import static org.mockito.Mockito.verify;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.sql.Timestamp;31import java.util.Date;32import java.util.stream.Stream;33import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;34import org.assertj.core.internal.objects.data.AlwaysEqualPerson;35import org.assertj.core.internal.objects.data.FriendlyPerson;36import org.assertj.core.internal.objects.data.Giant;37import org.assertj.core.internal.objects.data.Human;38import org.assertj.core.internal.objects.data.Person;39import org.junit.jupiter.api.DisplayName;40import org.junit.jupiter.api.Test;41import org.junit.jupiter.api.condition.DisabledOnOs;42import org.junit.jupiter.params.ParameterizedTest;43import org.junit.jupiter.params.provider.Arguments;44import org.junit.jupiter.params.provider.MethodSource;45@DisplayName("RecursiveComparisonAssert isEqualTo")46class RecursiveComparisonAssert_isEqualTo_Test extends RecursiveComparisonAssert_isEqualTo_BaseTest {47 @Test48 void should_pass_when_actual_and_expected_are_null() {49 // GIVEN50 Person actual = null;51 Person expected = null;52 // THEN53 assertThat(actual).usingRecursiveComparison()54 .isEqualTo(expected);55 }56 @Test57 void should_fail_when_actual_is_null_and_expected_is_not() {58 // GIVEN59 Person actual = null;60 Person expected = new Person();61 // WHEN62 compareRecursivelyFailsAsExpected(actual, expected);63 // THEN64 verify(failures).failure(info, shouldNotBeNull());65 }66 @Test67 void should_fail_when_actual_is_not_null_and_expected_is() {68 // GIVEN69 Person actual = new Person();70 Person expected = null;71 // WHEN72 compareRecursivelyFailsAsExpected(actual, expected);73 // THEN74 verify(failures).failure(info, shouldBeEqual(actual, null, objects.getComparisonStrategy(), info.representation()));75 }76 @Test77 void should_propagate_comparators_by_type() {78 // GIVEN79 Person actual = new Person("John");80 // WHEN81 RecursiveComparisonConfiguration assertion = assertThat(actual).usingComparatorForType(ALWAY_EQUALS_STRING, String.class)82 .usingRecursiveComparison()83 .getRecursiveComparisonConfiguration();84 // THEN85 assertThat(assertion.comparatorByTypes()).contains(entry(String.class, ALWAY_EQUALS_STRING));86 }87 @Test88 void should_not_use_equal_implementation_of_root_objects_to_compare() {89 // GIVEN90 AlwaysEqualPerson actual = new AlwaysEqualPerson();91 actual.name = "John";92 actual.home.address.number = 1;93 AlwaysEqualPerson expected = new AlwaysEqualPerson();94 expected.name = "John";95 expected.home.address.number = 2;96 // WHEN97 compareRecursivelyFailsAsExpected(actual, expected);98 // THEN99 ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);100 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference);101 }102 @Test103 void should_treat_date_as_equal_to_timestamp() {104 // GIVEN105 Person actual = new Person("Fred");106 actual.dateOfBirth = new Date(1000L);107 Person expected = new Person("Fred");108 expected.dateOfBirth = new Timestamp(1000L);109 // THEN110 assertThat(actual).usingRecursiveComparison()111 .isEqualTo(expected);112 }113 @Test114 void should_be_able_to_compare_objects_with_percentages() {115 // GIVEN116 Person actual = new Person("foo");117 Person expected = new Person("%foo");118 // WHEN119 compareRecursivelyFailsAsExpected(actual, expected);120 // THEN121 ComparisonDifference nameDifference = diff("name", actual.name, expected.name);122 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, nameDifference);123 }124 @Test125 void should_fail_when_fields_of_different_nesting_levels_differ() {126 // GIVEN127 Person actual = new Person("John");128 actual.home.address.number = 1;129 Person expected = new Person("Jack");130 expected.home.address.number = 2;131 // WHEN132 compareRecursivelyFailsAsExpected(actual, expected);133 // THEN134 ComparisonDifference nameDifference = diff("name", actual.name, expected.name);135 ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);136 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, nameDifference);137 }138 @SuppressWarnings("unused")139 @ParameterizedTest(name = "{2}: actual={0} / expected={1}")140 @MethodSource("recursivelyEqualObjects")141 void should_pass_for_objects_with_the_same_data_when_using_the_default_recursive_comparison(Object actual,142 Object expected,143 String testDescription) {144 assertThat(actual).usingRecursiveComparison()145 .isEqualTo(expected);146 }147 private static Stream<Arguments> recursivelyEqualObjects() {148 Person person1 = new Person("John");149 person1.home.address.number = 1;150 Person person2 = new Person("John");151 person2.home.address.number = 1;152 Person person3 = new Person("John");153 person3.home.address.number = 1;154 Human person4 = new Human();155 person4.name = "John";156 person4.home.address.number = 1;157 return Stream.of(arguments(person1, person2, "same data, same type"),158 arguments(person2, person1, "same data, same type reversed"),159 arguments(person3, person4, "same data, different type"),160 arguments(new Theme(RED), new Theme(RED), "same data with enum overriding methods - covers #1866"),161 arguments(person4, person3, "same data, different type"));162 }163 @Test164 void should_be_able_to_compare_objects_with_direct_cycles() {165 // GIVEN166 Person actual = new Person("John");167 actual.home.address.number = 1;168 Person expected = new Person("John");169 expected.home.address.number = 1;170 // neighbour171 expected.neighbour = actual;172 actual.neighbour = expected;173 // THEN174 assertThat(actual).usingRecursiveComparison()175 .isEqualTo(expected);176 }177 @Test178 void should_be_able_to_compare_objects_with_cycles_in_ordered_collection() {179 // GIVEN180 FriendlyPerson actual = new FriendlyPerson();181 actual.name = "John";182 actual.home.address.number = 1;183 FriendlyPerson expected = new FriendlyPerson();184 expected.name = "John";185 expected.home.address.number = 1;186 // neighbour187 expected.neighbour = actual;188 actual.neighbour = expected;189 // friends190 FriendlyPerson sherlock = new FriendlyPerson();191 sherlock.name = "Sherlock";192 sherlock.home.address.number = 221;193 actual.friends.add(sherlock);194 actual.friends.add(expected);195 expected.friends.add(sherlock);196 expected.friends.add(actual);197 // THEN198 assertThat(actual).usingRecursiveComparison()199 .isEqualTo(expected);200 }201 @Test202 void should_be_able_to_compare_objects_with_cycles_in_ordered_and_unordered_collection() {203 // GIVEN204 FriendlyPerson actual = new FriendlyPerson();205 actual.name = "John";206 actual.home.address.number = 1;207 FriendlyPerson expected = new FriendlyPerson();208 expected.name = "John";209 expected.home.address.number = 1;210 // neighbour - direct cycle211 expected.neighbour = actual;212 actual.neighbour = expected;213 // friends cycle with intermediate collection214 FriendlyPerson sherlock = new FriendlyPerson();215 sherlock.name = "Sherlock";216 sherlock.home.address.number = 221;217 // ordered collections218 actual.friends.add(sherlock);219 actual.friends.add(expected);220 expected.friends.add(sherlock);221 expected.friends.add(actual);222 // unordered collections223 // this could cause an infinite recursion if we don't track correctly the visited objects224 actual.otherFriends.add(actual);225 actual.otherFriends.add(expected);226 actual.otherFriends.add(sherlock);227 expected.otherFriends.add(sherlock);228 expected.otherFriends.add(expected);229 expected.otherFriends.add(actual);230 // THEN231 assertThat(actual).usingRecursiveComparison()232 .isEqualTo(expected);233 }234 @Test235 void should_report_difference_in_collection() {236 // GIVEN237 FriendlyPerson actual = new FriendlyPerson();238 FriendlyPerson actualFriend = new FriendlyPerson();239 actualFriend.home.address.number = 99;240 actual.friends = list(actualFriend);241 FriendlyPerson expected = new FriendlyPerson();242 FriendlyPerson expectedFriend = new FriendlyPerson();243 expectedFriend.home.address.number = 10;244 expected.friends = list(expectedFriend);245 // WHEN246 compareRecursivelyFailsAsExpected(actual, expected);247 // THEN248 ComparisonDifference friendNumberDifference = diff("friends.home.address.number", 99, 10);249 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, friendNumberDifference);250 }251 @Test252 void should_report_missing_property() {253 // GIVEN254 Giant actual = new Giant();255 actual.name = "joe";256 actual.height = 3.0;257 Human expected = new Human();258 expected.name = "joe";259 // WHEN260 compareRecursivelyFailsAsExpected(actual, expected);261 // THEN262 ComparisonDifference missingFieldDifference = diff("", actual, expected,263 "org.assertj.core.internal.objects.data.Giant can't be compared to org.assertj.core.internal.objects.data.Human as Human does not declare all Giant fields, it lacks these: [height]");264 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, missingFieldDifference);265 }266 @Test267 void should_not_compare_enum_recursively() {268 // GIVEN269 Light actual = new Light(GREEN);270 Light expected = new Light(BLUE);271 // WHEN272 compareRecursivelyFailsAsExpected(actual, expected);273 // THEN274 ComparisonDifference difference = diff("color", actual.color, expected.color);275 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);276 }277 @Test278 void should_compare_enum_by_value_only_when_strictTypeChecking_mode_is_disabled() {279 // GIVEN280 Light actual = new Light(GREEN);281 LightDto expected = new LightDto(ColorDto.GREEN);282 // WHEN-THEN283 then(actual).usingRecursiveComparison()284 .isEqualTo(expected);285 }286 @Test287 void should_fail_when_expected_is_an_enum_and_actual_is_not() {288 // GIVEN289 LightString actual = new LightString("GREEN");290 Light expected = new Light(GREEN);291 // WHEN292 compareRecursivelyFailsAsExpected(actual, expected);293 // THEN294 ComparisonDifference difference = diff("color", "GREEN", GREEN,295 "expected field is an enum but actual field is not (java.lang.String)");296 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);297 }298 @Test299 void should_fail_when_actual_is_an_enum_and_expected_is_not() {300 // GIVEN301 Employee devPerson = new Employee("Example Name", "SOFTWARE_DEVELOPER");302 BlogPost devBlogPost = new BlogPost(devPerson);303 EmployeeDTO qaPersonDTO = new EmployeeDTO("Example Name", QA_ENGINEER);304 BlogPostDTO qaBlogPostDTO = new BlogPostDTO(qaPersonDTO);305 // WHEN306 compareRecursivelyFailsAsExpected(qaBlogPostDTO, devBlogPost);307 // THEN308 ComparisonDifference difference = diff("author.jobTitle", QA_ENGINEER, "SOFTWARE_DEVELOPER");309 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(qaBlogPostDTO, devBlogPost, difference);310 }311 static class LightString {312 public String color;313 public LightString(String value) {314 this.color = value;315 }316 }317 @Test318 @DisabledOnOs(WINDOWS)319 void should_not_treat_Path_as_Iterable_to_avoid_infinite_recursion() {320 final Container container1 = new Container("/tmp/example");321 final Container container2 = new Container("/tmp/example");322 assertThat(container1).usingRecursiveComparison()323 .isEqualTo(container2)324 .ignoringAllOverriddenEquals()325 .isEqualTo(container2);326 }327 public static class Container {328 private Path path;329 public Container(String path) {330 this.path = Paths.get(path);331 }332 public Path getPath() {333 return path;334 }335 }336 public static class BlogPost {337 Employee author;338 public BlogPost(Employee author) {339 this.author = author;340 }341 }342 public static class BlogPostDTO {343 EmployeeDTO author;344 public BlogPostDTO(EmployeeDTO author) {345 this.author = author;346 }347 }348 public static class Employee {349 String name;350 String jobTitle;351 public Employee(String name, String jobTitle) {352 this.name = name;353 this.jobTitle = jobTitle;354 }355 }356 public static class EmployeeDTO {357 String name;358 JobTitle jobTitle;359 public EmployeeDTO(String name, JobTitle jobTitle) {360 this.name = name;361 this.jobTitle = jobTitle;362 }363 public enum JobTitle {364 SOFTWARE_DEVELOPER, QA_ENGINEER365 }366 }367}...

Full Screen

Full Screen

Source:RecursiveAssertionsExamples.java Github

copy

Full Screen

...96 }97 @Test98 public void recursive_field_by_field_comparison_assertions_with_optional_examples() {99 // GIVEN100 Song song = new Song("I Can't Get No Satisfaction", new Author("Mick Jagger"), new Author("Keith Richards"));101 Song expectedSong = new Song("I Can't Get No Satisfaction", new Author("Mick Jagger"), new Author("Keith Richards"));102 // FAIL103 assertThat(song).usingRecursiveComparison()104 .isEqualTo(expectedSong);105 }106 @Test107 public void recursive_field_by_field_comparison_assertions_ignoring_collection_order_examples() {108 // GIVEN109 Song song = new Song("I Can't Get No Satisfaction", new Author("Mick Jagger"), new Author("Keith Richards"));110 Song expectedSong = new Song("I Can't Get No Satisfaction", new Author("Mick Jagger"), new Author("Keith Richards"));111 // FAIL112 assertThat(song).usingRecursiveComparison()113 .isEqualTo(expectedSong);114 }115 static class Song {116 public Author author;117 public Optional<Author> coAuthor;118 public String song;119 public Song(String song, Author author, Author coAuthor) {120 this.song = song;121 this.author = author;122 this.coAuthor = Optional.ofNullable(coAuthor);123 }124 @Override125 public String toString() {126 return String.format("Song [author=%s, coAuthor=%s, song=%s]", author, coAuthor, song);127 }128 }129 class Author {130 String name;131 public Author(String name) {132 this.name = name;133 }134 public String getName() {135 return name;136 }137 @Override138 public String toString() {139 return name;140 }141 }142 @Test143 public void usingFieldByFieldElementComparatorTest() throws Exception {144 List<Animal> animals = new ArrayList<>();145 Bird bird = new Bird("White");...

Full Screen

Full Screen

Source:IterableAssert_usingRecursiveFieldByFieldElementComparator_with_RecursiveComparisonConfiguration_Test.java Github

copy

Full Screen

1/*2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with3 * the License. You may obtain a copy of the License at4 *5 * http://www.apache.org/licenses/LICENSE-2.06 *7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the9 * specific language governing permissions and limitations under the License.10 *11 * Copyright 2012-2020 the original author or authors.12 */13package org.assertj.core.api.iterable;14import static java.util.Collections.singletonList;15import static org.assertj.core.api.BDDAssertions.then;16import org.assertj.core.api.ConcreteIterableAssert;17import org.assertj.core.api.IterableAssertBaseTest;18import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;19import org.assertj.core.internal.ComparatorBasedComparisonStrategy;20import org.assertj.core.internal.ConfigurableRecursiveFieldByFieldComparator;21import org.assertj.core.internal.IterableElementComparisonStrategy;22import org.assertj.core.internal.Iterables;23import org.junit.jupiter.api.BeforeEach;24import org.junit.jupiter.api.Test;25class IterableAssert_usingRecursiveFieldByFieldElementComparator_with_RecursiveComparisonConfiguration_Test extends IterableAssertBaseTest {26 private Iterables iterablesBefore;27 private RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();28 @BeforeEach29 void before() {30 iterablesBefore = getIterables(assertions);31 }32 @Override33 protected ConcreteIterableAssert<Object> invoke_api_method() {34 return assertions.usingRecursiveFieldByFieldElementComparator(recursiveComparisonConfiguration);35 }36 @Override37 protected void verify_internal_effects() {38 then(iterablesBefore).isNotSameAs(getIterables(assertions));39 then(getIterables(assertions).getComparisonStrategy()).isInstanceOf(ComparatorBasedComparisonStrategy.class);40 then(getObjects(assertions).getComparisonStrategy()).isInstanceOf(IterableElementComparisonStrategy.class);41 ConfigurableRecursiveFieldByFieldComparator expectedComparator = new ConfigurableRecursiveFieldByFieldComparator(recursiveComparisonConfiguration);42 then(getIterables(assertions).getComparator()).isEqualTo(expectedComparator);43 then(getObjects(assertions).getComparisonStrategy()).extracting("elementComparator").isEqualTo(expectedComparator);44 }45 @Test46 void should_be_able_to_use_specific_RecursiveComparisonConfiguration_when_using_recursive_field_by_field_element_comparator() {47 // GIVEN48 Foo actual = new Foo("1", new Bar(1));49 Foo other = new Foo("2", new Bar(1));50 RecursiveComparisonConfiguration configuration = new RecursiveComparisonConfiguration();51 configuration.ignoreFields("id");52 // WHEN/THEN53 then(singletonList(actual)).usingRecursiveFieldByFieldElementComparator(configuration)54 .contains(other);55 }56 public static class Foo {57 public String id;58 public Bar bar;59 public Foo(String id, Bar bar) {60 this.id = id;61 this.bar = bar;62 }63 @Override64 public String toString() {65 return "Foo(id=" + id + ", bar=" + bar + ")";66 }67 }68 public static class Bar {69 public int id;70 public Bar(int id) {71 this.id = id;72 }73 @Override74 public String toString() {75 return "Bar(id=" + id + ")";76 }77 }78}...

Full Screen

Full Screen

Author

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.Author;2import org.assertj.core.api.recursive.comparison.AuthorAssert;3public class AuthorTest {4 public static void main(String[] args) {5 Author author = new Author();6 author.setFirstName("John");7 author.setLastName("Smith");8 author.setAge(45);9 author.setCity("London");10 AuthorAssert.assertThat(author).hasAge(45);11 }12}13 <Author(firstName=John, lastName=Smith, age=45, city=London)>14 at org.assertj.core.api.recursive.comparison.AuthorAssert.hasAge(AuthorAssert.java:33)15 at AuthorTest.main(AuthorTest.java:10)16package org.assertj.core.api.recursive.comparison;17public class Author {18 private String firstName;19 private String lastName;20 private int age;21 private String city;22 public String getFirstName() {23 return firstName;24 }25 public void setFirstName(String firstName) {26 this.firstName = firstName;27 }28 public String getLastName() {29 return lastName;30 }31 public void setLastName(String lastName) {32 this.lastName = lastName;33 }34 public int getAge() {35 return age;36 }37 public void setAge(int age) {38 this.age = age;39 }40 public String getCity() {41 return city;42 }43 public void setCity(String city) {44 this.city = city;45 }46}

Full Screen

Full Screen

Author

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.Author;2public class AuthorTest {3 public static void main(String args[]) {4 Author author = new Author("John", "Doe");5 System.out.println(author);6 }7}

Full Screen

Full Screen

Author

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.Author;2public class 1 {3public static void main(String[] args) {4Author author = new Author("John", "Doe");5System.out.println("Author: " + author);6}7}

Full Screen

Full Screen

Author

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.*;2import org.assertj.core.api.recursive.comparison.Author;3import org.junit.Test;4public class PathTest {5 public void test() {6 assertThat(new Author("John Doe")).isEqualTo(new Author("John Doe"));7 }8}9 at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:304)10 at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert.isEqualTo(RecursiveComparisonAssert.java:46)11 at PathTest.test(PathTest.java:9)12The problem is that the path is not set when comparing the root object. This is because the path is set in the recursive call to compareFieldsAndElementsOfActualAndExpected() in the if block:13if (isNotSameInstanceAsActual(actual, expected)) {14 compareFieldsAndElementsOfActualAndExpected(context, actual, expected, comparisonStrategy);15}16I think that if the if block is moved out of the isNotSameInstanceAsActual() method, it will work:17if (isNotSameInstanceAsActual(actual, expected)) {18 compareFieldsAndElementsOfActualAndExpected(context, actual, expected, comparisonStrategy);19} else {20 compareRootObjects(context, actual, expected, comparisonStrategy);21}

Full Screen

Full Screen

Author

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.Author;2public class Example{3 public static void main(String[] args){4 Author a=new Author("John","Doe");5 System.out.println(a);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.

Run Assertj automation tests on LambdaTest cloud grid

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

Most used methods in Author

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful