public AssertionError failureIfErrorMessageIsOverridden(AssertionInfo info) { String overridingErrorMessage = info.overridingErrorMessage(); return isNullOrEmpty(overridingErrorMessage) ? null : failure( MessageFormatter.instance() .format(info.description(), info.representation(), overridingErrorMessage)); }
/** * Creates a <code>{@link AssertionError}</code> following this pattern: * * <ol> * <li>creates a <code>{@link AssertionError}</code> using <code> * {@link AssertionInfo#overridingErrorMessage()}</code> as the error message if such value * is not {@code null}, or * <li>uses the given <code>{@link ErrorMessageFactory}</code> to create the detail message of * the <code>{@link AssertionError}</code>, prepending the value of <code> * {@link AssertionInfo#description()}</code> to the error message * </ol> * * @param info contains information about the failed assertion. * @param message knows how to create detail messages for {@code AssertionError}s. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(AssertionInfo info, ErrorMessageFactory message) { AssertionError error = failureIfErrorMessageIsOverridden(info); if (error != null) return error; AssertionError assertionError = new AssertionError(message.create(info.description(), info.representation())); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
@Test public void should_fail_if_doubles_are_not_equal() { AssertionInfo info = someInfo(); try { doubles.assertEqual(info, 6d, 8d); } catch (AssertionError e) { verify(failures).failure(info, shouldBeEqual(6d, 8d, info.representation())); return; } failBecauseExpectedAssertionErrorWasNotThrown(); }
@Test public void should_fail_if_actual_size_is_not_equal_to_other_size() { AssertionInfo info = someInfo(); String[] actual = array("Yoda"); List<String> other = newArrayList("Solo", "Leia"); try { arrays.assertHasSameSizeAs(info, actual, other); } catch (AssertionError e) { assertThat(e) .hasMessage( shouldHaveSameSizeAs(actual, actual.length, other.size()) .create(null, info.representation())); return; } failBecauseExpectedAssertionErrorWasNotThrown(); }
/** * Creates a <code>{@link AssertionError}</code> following this pattern: * * <ol> * <li>creates a <code>{@link AssertionError}</code> using <code> * {@link AssertionInfo#overridingErrorMessage()}</code> as the error message if such value * is not {@code null}, or * <li>uses the given <code>{@link AssertionErrorFactory}</code> to create an <code> * {@link AssertionError}</code>, prepending the value of <code> * {@link AssertionInfo#description()}</code> to the error message * </ol> * * @param info contains information about the failed assertion. * @param factory knows how to create {@code AssertionError}s. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(AssertionInfo info, AssertionErrorFactory factory) { AssertionError error = failureIfErrorMessageIsOverridden(info); if (error != null) return error; printThreadDumpIfNeeded(); return factory.newAssertionError(info.description(), info.representation()); }