Best Assertj code snippet using org.assertj.core.error.DescriptionFormatter.instance
Source:ShouldBeEqual_newAssertionError_differentiating_expected_and_actual_Test.java
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-2022 the original author or authors.12 */13package org.assertj.core.error;14import static java.lang.Integer.toHexString;15import static java.lang.String.format;16import static org.assertj.core.api.BDDAssertions.then;17import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;18import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;19import static org.assertj.core.util.Strings.concat;20import static org.mockito.Mockito.mock;21import static org.mockito.Mockito.when;22import java.util.Comparator;23import org.assertj.core.description.Description;24import org.assertj.core.internal.ComparatorBasedComparisonStrategy;25import org.assertj.core.internal.ComparisonStrategy;26import org.assertj.core.internal.TestDescription;27import org.junit.jupiter.api.BeforeEach;28import org.junit.jupiter.api.Test;29import org.opentest4j.AssertionFailedError;30/**31 * Tests for32 * <code>{@link ShouldBeEqual#newAssertionError(Description, org.assertj.core.presentation.Representation)}</code>.33 *34 * @author Joel Costigliola (based on Tomasz Nurkiewicz ideas)35 */36class ShouldBeEqual_newAssertionError_differentiating_expected_and_actual_Test {37 private String formattedDescription = "[my test]";38 private Description description;39 @BeforeEach40 public void setUp() {41 description = new TestDescription("my test");42 }43 @Test44 void should_create_AssertionError_with_message_differentiating_expected_double_and_actual_float() {45 // GIVEN46 Float actual = 42f;47 Double expected = 42d;48 ShouldBeEqual shouldBeEqual = (ShouldBeEqual) shouldBeEqual(actual, expected, STANDARD_REPRESENTATION);49 shouldBeEqual.descriptionFormatter = mock(DescriptionFormatter.class);50 when(shouldBeEqual.descriptionFormatter.format(description)).thenReturn(formattedDescription);51 // WHEN52 AssertionError error = shouldBeEqual.newAssertionError(description, STANDARD_REPRESENTATION);53 // THEN54 then(error).isInstanceOf(AssertionFailedError.class)55 .hasMessage(format("[my test] %n" +56 "expected: 42.0%n" +57 " but was: 42.0f"));58 }59 @Test60 void should_create_AssertionError_with_message_differentiating_expected_and_actual_persons() {61 // GIVEN62 Person actual = new Person("Jake", 43);63 Person expected = new Person("Jake", 47);64 ShouldBeEqual shouldBeEqual = (ShouldBeEqual) shouldBeEqual(actual, expected, STANDARD_REPRESENTATION);65 shouldBeEqual.descriptionFormatter = mock(DescriptionFormatter.class);66 when(shouldBeEqual.descriptionFormatter.format(description)).thenReturn(formattedDescription);67 // WHEN68 AssertionError error = shouldBeEqual.newAssertionError(description, STANDARD_REPRESENTATION);69 // THEN70 then(error).isInstanceOf(AssertionFailedError.class)71 .hasMessage("[my test] %n" +72 "expected: \"Person[name=Jake] (Person@%s)\"%n" +73 " but was: \"Person[name=Jake] (Person@%s)\"",74 toHexString(expected.hashCode()), toHexString(actual.hashCode()));75 }76 @Test77 void should_create_AssertionError_with_message_differentiating_expected_and_actual_persons_even_if_a_comparator_based_comparison_strategy_is_used() {78 // GIVEN79 Person actual = new Person("Jake", 43);80 Person expected = new Person("Jake", 47);81 ComparisonStrategy ageComparisonStrategy = new ComparatorBasedComparisonStrategy(new PersonComparator());82 ShouldBeEqual shouldBeEqual = (ShouldBeEqual) shouldBeEqual(actual, expected, ageComparisonStrategy, STANDARD_REPRESENTATION);83 shouldBeEqual.descriptionFormatter = mock(DescriptionFormatter.class);84 when(shouldBeEqual.descriptionFormatter.format(description)).thenReturn(formattedDescription);85 // WHEN86 AssertionError error = shouldBeEqual.newAssertionError(description, STANDARD_REPRESENTATION);87 // THEN88 then(error).isInstanceOf(AssertionFailedError.class)89 .hasMessage("[my test] %n" +90 "expected: \"Person[name=Jake] (Person@%s)\"%n" +91 " but was: \"Person[name=Jake] (Person@%s)\"%n" +92 "when comparing values using PersonComparator",93 toHexString(expected.hashCode()), toHexString(actual.hashCode()));94 }95 @Test96 void should_create_AssertionError_with_message_differentiating_null_and_object_with_null_toString() {97 // GIVEN98 Object actual = null;99 Object expected = new ToStringIsNull();100 ShouldBeEqual shouldBeEqual = (ShouldBeEqual) shouldBeEqual(actual, expected, STANDARD_REPRESENTATION);101 shouldBeEqual.descriptionFormatter = mock(DescriptionFormatter.class);102 when(shouldBeEqual.descriptionFormatter.format(description)).thenReturn(formattedDescription);103 // WHEN104 AssertionError error = shouldBeEqual.newAssertionError(description, STANDARD_REPRESENTATION);105 // THEN106 then(error).isInstanceOf(AssertionFailedError.class)107 .hasMessage("[my test] %n" +108 "expected: \"null (ToStringIsNull@%s)\"%n" +109 " but was: null",110 toHexString(expected.hashCode()));111 }112 @Test113 void should_create_AssertionError_with_message_differentiating_object_with_null_toString_and_null() {114 // GIVEN115 Object actual = new ToStringIsNull();116 Object expected = null;117 ShouldBeEqual shouldBeEqual = (ShouldBeEqual) shouldBeEqual(actual, expected, STANDARD_REPRESENTATION);118 shouldBeEqual.descriptionFormatter = mock(DescriptionFormatter.class);119 when(shouldBeEqual.descriptionFormatter.format(description)).thenReturn(formattedDescription);120 // WHEN121 AssertionError error = shouldBeEqual.newAssertionError(description, STANDARD_REPRESENTATION);122 // THEN123 then(error).isInstanceOf(AssertionFailedError.class)124 .hasMessage("[my test] %n" +125 "expected: null%n" +126 " but was: \"null (ToStringIsNull@%s)\"",127 toHexString(actual.hashCode()));128 }129 private static class Person {130 private final String name;131 private final int age;132 public Person(String name, int age) {133 this.name = name;134 this.age = age;135 }136 @Override137 public String toString() {138 return concat("Person[name=", name, "]");139 }140 }141 private static class PersonComparator implements Comparator<Person> {142 @Override143 public int compare(Person p1, Person p2) {144 return p1.age - p2.age;145 }146 }147 public static class ToStringIsNull {148 @Override149 public String toString() {150 return null;151 }152 }153}...
instance
Using AI Code Generation
1DescriptionFormatter descriptionFormatter = new DescriptionFormatter();2String description = descriptionFormatter.format(descriptionFormatter.instance(), "test");3System.out.println(description);4description = descriptionFormatter.format(descriptionFormatter.method(), "test");5System.out.println(description);6description = descriptionFormatter.format(descriptionFormatter.constructor(), "test");7System.out.println(description);8description = descriptionFormatter.format(descriptionFormatter.type(), "test");9System.out.println(description);10description = descriptionFormatter.format(descriptionFormatter.values(), "test");11System.out.println(description);12description = descriptionFormatter.format(descriptionFormatter.value(), "test");13System.out.println(description);14description = descriptionFormatter.format(descriptionFormatter.expected(), "test");15System.out.println(description);16description = descriptionFormatter.format(descriptionFormatter.actual(), "test");17System.out.println(description);18description = descriptionFormatter.format(descriptionFormatter.element(), "test");19System.out.println(description);20description = descriptionFormatter.format(descriptionFormatter.key(), "test");21System.out.println(description);22description = descriptionFormatter.format(descriptionFormatter.description(), "test");23System.out.println(description);24description = descriptionFormatter.format(descriptionFormatter.parameter(), "test");25System.out.println(description);26description = descriptionFormatter.format(descriptionFormatter.index(), "test");27System.out.println(description);
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!!