/** * Sets whether we remove elements related to AssertJ from assertion error stack trace. * * @param removeAssertJRelatedElementsFromStackTrace flag. */ public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace) { Failures.instance().setRemoveAssertJRelatedElementsFromStackTrace(removeAssertJRelatedElementsFromStackTrace); }
/** * Throws an {@link AssertionError} with the given message. * * @param <T> dummy return value type * @param failureMessage error message. * @return nothing, it's just to be used in doSomething(optional.orElse(() -> fail("boom")));. * @throws AssertionError with the given message. */ public static <T> T fail(String failureMessage) { throw Failures.instance().failure(failureMessage); }
@VisibleForTesting void resetFailures() { this.failures = Failures.instance(); }
/** * Throws an {@link AssertionError} with a message explaining that a {@link Throwable} of given class was expected to be thrown * but had not been. * * @param <T> dummy return value type * @param throwableClass the Throwable class that was expected to be thrown. * @return nothing, it's just to be used in doSomething(optional.orElse(() -> shouldHaveThrown(IOException.class)));. * @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had * not been. */ public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass) { throw Failures.instance().expectedThrowableNotThrown(throwableClass); }
private SELF matches(Predicate<? super ACTUAL> predicate, PredicateDescription predicateDescription) { requireNonNull(predicate, "The predicate must not be null"); if (predicate.test(actual)) return myself; throw Failures.instance().failure(info, shouldMatch(actual, predicate, predicateDescription)); }
/** * Fails with the given message and with the {@link Throwable} that caused the failure. * * @param failureMessage error message. * @param realCause cause of the error. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage, Throwable realCause) { AssertionError error = Failures.instance().failure(failureMessage); error.initCause(realCause); proxies.collectError(error); }
/** * Fails with the given message built like {@link String#format(String, Object...)}. * * @param failureMessage error message. * @param args Arguments referenced by the format specifiers in the format string. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage, Object... args) { AssertionError error = Failures.instance().failure(String.format(failureMessage, args)); proxies.collectError(error); }
/** {@inheritDoc} */ @Override @CheckReturnValue public SELF withThreadDumpOnError() { Failures.instance().enablePrintThreadDump(); return myself; }
/** * Fails with the given message. * * @param failureMessage error message. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage) { AssertionError error = Failures.instance().failure(failureMessage); proxies.collectError(error); }
private SELF isCompletedWithValueMatching(Predicate<? super RESULT> predicate, PredicateDescription description) { isCompleted(); RESULT actualResult = actual.join(); if (!predicate.test(actualResult)) throw Failures.instance().failure(info, shouldMatch(actualResult, predicate, description)); return myself; }
protected SELF hasBeenThrown() { if (actual == null) { throw Failures.instance().failure(info, new BasicErrorMessageFactory("%nExpecting code to raise a throwable.")); } return myself; }
/** * Verifies that the {@link org.assertj.core.api.ThrowableAssert.ThrowingCallable} didn't raise a throwable. * <p> * Example : * <pre><code class='java'> assertThatCode(() -> foo.bar()).doesNotThrowAnyException();</code></pre> * * @throws AssertionError if the actual statement raised a {@code Throwable}. * @since 3.7.0 */ public void doesNotThrowAnyException() { if (actual != null) throw Failures.instance().failure(info, shouldNotHaveThrown(actual)); } }
private void assertIsPrimitiveZero() { if (actual.floatValue() == 0.0f) return; throw Failures.instance().failure(info, shouldBeEqual(actual, 0.0f, info.representation())); }
private AssertionError comparisonFailure(Description description) { try { AssertionError comparisonFailure = newComparisonFailure(descriptionFormatter.format(description).trim()); Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(comparisonFailure); return comparisonFailure; } catch (Throwable e) { return null; } }
private void assertIsPrimitiveZero() { if (actual.doubleValue() == 0.0) return; throw Failures.instance().failure(info, shouldBeEqual(actual, 0.0, info.representation())); }
private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) { if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return; if (isAssertjAssertClass()) return; StackTraceElement[] newStackTrace = Arrays.stream(assertionError.getStackTrace()) .filter(element -> !isElementOfCustomAssert(element)) .toArray(StackTraceElement[]::new); assertionError.setStackTrace(newStackTrace); }
public SELF hasNoBody() { byte[] contentAsByteArray = actual.getContentAsByteArray(); if (!(contentAsByteArray == null || contentAsByteArray.length == 0)) { throw Failures.instance().failure(info, shouldBeNullOrEmpty(contentAsByteArray)); } return myself; }
private AssertionError actualDoesNotStartWithSequence(AssertionInfo info, Object[] sequence) { return Failures.instance() .failure(info, shouldStartWith("Stream under test", sequence, iterables.getComparisonStrategy())); }
public SELF hasBody(byte[] expected) { if (!Objects.areEqual(actual.getContentAsByteArray(), expected)) { this.as("body"); throw Failures.instance().failure(info, shouldBeEqual(actual.getContentAsByteArray(), expected, info.representation())); } return myself; }
public SELF hasBody(String expected) throws UnsupportedEncodingException { if (!Objects.areEqual(actual.getContentAsString(), expected)) { this.as("body"); throw Failures.instance().failure(info, shouldBeEqual(actual.getContentAsString(), expected, info.representation())); } return myself; }