Best Assertj code snippet using org.assertj.core.error.ShouldBeEqual.smartErrorMessage
Source:ShouldBeEqual.java
...103 * @return the created {@code AssertionError}.104 */105 @Override106 public AssertionError newAssertionError(Description description, Representation representation) {107 String message = smartErrorMessage(description, representation);108 // only use JUnit error message if the comparison strategy used was standard, otherwise we need to mention109 // comparison strategy in the assertion error message to make it clear to the user it was used.110 if (comparisonStrategy.isStandard() && !actualAndExpectedHaveSameStringRepresentation()) {111 // comparison strategy is standard -> try to build an AssertionFailedError used in JUnit 5 that is nicely displayed in IDEs112 AssertionError assertionFailedError = assertionFailedError(message, representation);113 // assertionFailedError != null means that JUnit 5 and opentest4j are in the classpath114 if (assertionFailedError != null) return assertionFailedError;115 // Junit5 was not used, try to build a JUnit 4 ComparisonFailure that is nicely displayed in IDEs116 AssertionError error = comparisonFailure(description);117 // error != null means that JUnit 4 was in the classpath and we build a ComparisonFailure.118 if (error != null) return error;119 }120 AssertionError assertionFailedError = assertionFailedError(message, representation);121 // assertionFailedError != null means that JUnit 5 and opentest4j was in the classpath122 if (assertionFailedError != null) return assertionFailedError;123 // No JUnit in the classpath => fall back to default error message124 return Failures.instance().failure(message);125 }126 protected boolean actualAndExpectedHaveSameStringRepresentation() {127 return Objects.equals(representation.toStringOf(actual), representation.toStringOf(expected));128 }129 /**130 * Builds and returns an error message from the given description using {@link #expected} and {@link #actual} basic131 * representation if their description differ otherwise use132 * {@link #defaultDetailedErrorMessage(Description, Representation)} to represent them differently.133 *134 * @param description the {@link Description} used to build the returned error message135 * @param representation the {@link org.assertj.core.presentation.Representation} used to build String representation136 * of object137 * @return the error message from description using {@link #expected} and {@link #actual} "smart" representation.138 */139 protected String smartErrorMessage(Description description, Representation representation) {140 if (actualAndExpectedHaveSameStringRepresentation()) {141 // This happens for example when actual = 42f and expected = 42d, which will give this error:142 // actual : "42" and expected : "42".143 // JUnit 4 manages this case even worst, it will output something like :144 // "java.lang.String expected:java.lang.String<42.0> but was: java.lang.String<42.0>"145 // which makes things even more confusing since we lost the fact that 42 was a float or a double.146 // It is therefore better to built our own description without using ComparisonFailure, the147 // only drawback is that it won't look nice in IDEs.148 return defaultDetailedErrorMessage(description, representation);149 }150 return comparisonStrategy.isStandard()151 ? messageFormatter.format(description, representation, EXPECTED_BUT_WAS_MESSAGE, actual, expected)152 : messageFormatter.format(description, representation, EXPECTED_BUT_WAS_MESSAGE_USING_COMPARATOR,153 actual, expected, comparisonStrategy);...
smartErrorMessage
Using AI Code Generation
1 Object actual = 1;2 Object expected = 2;3 AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isEqualTo(expected));4 String errorMessage = smartErrorMessage(assertionError);5 assertThat(errorMessage).isEqualTo("expected:<2> but was:<1>");6 Object actual = 1;7 Object expected = 2;8 AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isEqualTo(expected));9 String errorMessage = smartErrorMessage(assertionError);10 assertThat(errorMessage).isEqualTo("expected:<2> but was:<1>");
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!!