void assertContainsOnlyNulls(AssertionInfo info, Failures failures, Object[] actual) { assertNotNull(info, actual); // empty => no null elements => failure if (actual.length == 0) throw failures.failure(info, shouldContainOnlyNulls(actual)); // look for any non null elements List<Object> nonNullElements = new ArrayList<>(); for (Object element : actual) { if (element != null) nonNullElements.add(element); } if (nonNullElements.size() > 0) throw failures.failure(info, shouldContainOnlyNulls(actual, nonNullElements)); }
/** * 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); }
private AssertionError assertionFailedError(String message) { try { Object o = constructorInvoker.newInstance("org.opentest4j.AssertionFailedError", MSG_ARG_TYPES_FOR_ASSERTION_FAILED_ERROR, message, expected, actual); if (o instanceof AssertionError) { AssertionError assertionError = (AssertionError) o; Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); return assertionError; } return null; } catch (Throwable e) { return null; } }
/** * 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 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); }
AssertionError assertionError = Failures.instance().failureIfErrorMessageIsOverridden(info); if (assertionError == null) { assertionError = new AssertionError(description + String.format(errorMessage, arguments)); Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); removeCustomAssertRelatedElementsFromStackTraceIfNeeded(assertionError); throw assertionError;
/** {@inheritDoc} */ @Override @CheckReturnValue public SELF withThreadDumpOnError() { Failures.instance().enablePrintThreadDump(); return myself; }
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); }
@VisibleForTesting void resetFailures() { this.failures = Failures.instance(); }
/** * 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> * <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</li> * </ol> * * @param info contains information about the failed assertion. * @param messageFactory knows how to create detail messages for {@code AssertionError}s. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory) { AssertionError error = failureIfErrorMessageIsOverridden(info); if (error != null) return error; AssertionError assertionError = new AssertionError(messageFactory.create(info.description(), info.representation())); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory, Object actual, Object expected) { String assertionErrorMessage = assertionErrorMessage(info, messageFactory); AssertionError assertionError = assertionError(assertionErrorMessage, actual, expected); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
/** * Creates a <code>{@link AssertionError}</code> using the given {@code String} as message. * <p> * It filters the AssertionError stack trace be default, to have full stack trace use * {@link #setRemoveAssertJRelatedElementsFromStackTrace(boolean)}. * * @param message the message of the {@code AssertionError} to create. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(String message) { AssertionError assertionError = new AssertionError(message); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
/** * 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> * <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</li> * </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()); }
/** * 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> * <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</li> * </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 = failureIfErrorMessageIsOverriden(info); if (error != null) return error; AssertionError assertionError = new AssertionError(message.create(info.description(), info.representation())); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); return assertionError; }
/** * Creates a <code>{@link AssertionError}</code> using the given {@code String} as message. * <p> * It filters the AssertionError stack trace be default, to have full stack trace use * {@link #setRemoveAssertJRelatedElementsFromStackTrace(boolean)}. * @param message the message of the {@code AssertionError} to create. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(String message) { AssertionError assertionError = new AssertionError(message); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); return assertionError; }
AssertionError assertionError = Failures.instance().failureIfErrorMessageIsOverridden(info); if (assertionError == null) { assertionError = new AssertionError(description + String.format(errorMessage, arguments)); Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); removeCustomAssertRelatedElementsFromStackTraceIfNeeded(assertionError); throw assertionError;
/** * 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); }
/** * 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> * <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</li> * </ol> * * @param info contains information about the failed assertion. * @param messageFactory knows how to create detail messages for {@code AssertionError}s. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory) { AssertionError error = failureIfErrorMessageIsOverridden(info); if (error != null) return error; AssertionError assertionError = new AssertionError(messageFactory.create(info.description(), info.representation())); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
public AssertionError failure(AssertionInfo info, ErrorMessageFactory messageFactory, Object actual, Object expected) { String assertionErrorMessage = assertionErrorMessage(info, messageFactory); AssertionError assertionError = assertionErrorCreator.assertionError(assertionErrorMessage, actual, expected); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }
/** * Creates a <code>{@link AssertionError}</code> using the given {@code String} as message. * <p> * It filters the AssertionError stack trace be default, to have full stack trace use * {@link #setRemoveAssertJRelatedElementsFromStackTrace(boolean)}. * * @param message the message of the {@code AssertionError} to create. * @return the created <code>{@link AssertionError}</code>. */ public AssertionError failure(String message) { AssertionError assertionError = new AssertionError(message); removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError); printThreadDumpIfNeeded(); return assertionError; }