/** * Modifies collected errors. Override to customize modification. * @param errors list of errors to decorate * @return decorated list */ protected List<Throwable> decorateErrorsCollected(List<Throwable> errors) { return addLineNumberToErrorMessages(errors); }
private void addLineNumberToErrorMessage(Throwable error) { StackTraceElement testStackTraceElement = getFirstStackTraceElementFromTest(error.getStackTrace()); if (testStackTraceElement != null) { changeErrorMessage(error, buildErrorMessageWithLineNumber(error.getMessage(), testStackTraceElement)); } }
/** * Returns a copy of list of soft assertions collected errors. * @return a copy of list of soft assertions collected errors. */ public List<Throwable> errorsCollected() { return decorateErrorsCollected(proxies.errorsCollected()); }
@Override public void evaluate() throws Throwable { baseStatement.evaluate(); List<Throwable> errors = soft.errorsCollected(); if (errors.isEmpty()) return; // tests assertions raised some errors tryThrowingMultipleFailuresError(errors); // failed to throw MultipleFailuresError -> throw MultipleFailureException instead MultipleFailureException.assertEmpty(errors); } };
/** * Creates a new instance of <code>{@link ByteAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public ByteAssert assertThat(byte actual) { return proxy(ByteAssert.class, Byte.class, actual); }
/** * Fails with a message explaining that a {@link Throwable} of given class was expected to be thrown * but had not been. * * @param throwableClass the Throwable class that was expected to be thrown. * @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had * not been. * @since 2.6.0 / 3.6.0 * * {@link Fail#shouldHaveThrown(Class)} can be used as a replacement. */ public void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) { shouldHaveThrown(throwableClass); }
private StackTraceElement getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace) { for (StackTraceElement element : stacktrace) { String className = element.getClassName(); if (isProxiedAssertionClass(className) || className.startsWith("sun.reflect") || className.startsWith("jdk.internal.reflect") || className.startsWith("java.") || className.startsWith("javax.") || className.startsWith("org.junit.") || className.startsWith("org.eclipse.jdt.internal.junit.") || className.startsWith("org.eclipse.jdt.internal.junit4.") || className.startsWith("org.eclipse.jdt.internal.junit5.") || className.startsWith("com.intellij.junit5.") || className.startsWith("com.intellij.rt.execution.junit.") || className.startsWith("org.apache.maven.surefire") || className.startsWith("org.assertj")) { continue; } return element; } return null; }
@Override public void evaluate() throws Throwable { baseStatement.evaluate(); List<Throwable> errors = soft.errorsCollected(); if (errors.isEmpty()) return; // tests assertions raised some errors assertionErrorCreator.tryThrowingMultipleFailuresError(errors); // failed to throw MultipleFailuresError -> throw MultipleFailureException instead MultipleFailureException.assertEmpty(errors); } };
/** * Creates a new instance of <code>{@link ThrowableAssert}</code> with the exception thrown by the given * {@link Callable} execution. * <p> * Example : * * <pre><code class='java'> * SoftAssertions softly = new SoftAssertions(); * softly.assertThatExceptionThrownBy(new Callable<Void>() * * {@literal @}Override * public Void call() throws Exception { * throw new Exception("something was wrong"); * } * * }).isInstanceOf(Exception.class) * .hasMessage("something was wrong"); * </code></pre> * * @param callable the callable whose execution throws an exception we want to check. * @return the created assertion object. */ public <V> ThrowableAssert assertThatExceptionThrownBy(Callable<V> callable) { return assertThat(new ThrowableAssert(callable).actual); } }
/** * Creates a new instance of <code>{@link DoubleAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public DoubleAssert assertThat(Double actual) { return proxy(DoubleAssert.class, Double.class, actual); }
/** * Fails with a message explaining that a {@link Throwable} of given class was expected to be thrown * but had not been. * * @param throwableClass the Throwable class that was expected to be thrown. * @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had * not been. * @since 2.6.0 / 3.6.0 * * {@link Fail#shouldHaveThrown(Class)} can be used as a replacement. */ public void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) { shouldHaveThrown(throwableClass); }
private StackTraceElement getFirstStackTraceElementFromTest(StackTraceElement[] stacktrace) { for (StackTraceElement element : stacktrace) { String className = element.getClassName(); if (isProxiedAssertionClass(className) || className.startsWith("sun.reflect") || className.startsWith("jdk.internal.reflect") || className.startsWith("java.") || className.startsWith("javax.") || className.startsWith("org.junit.") || className.startsWith("org.eclipse.jdt.internal.junit.") || className.startsWith("org.eclipse.jdt.internal.junit4.") || className.startsWith("org.eclipse.jdt.internal.junit5.") || className.startsWith("com.intellij.junit5.") || className.startsWith("com.intellij.rt.execution.junit.") || className.startsWith("org.apache.maven.surefire") || className.startsWith("org.assertj")) { continue; } return element; } return null; }
/** * Creates a new instance of <code>{@link InputStreamAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public InputStreamAssert assertThat(InputStream actual) { return proxy(InputStreamAssert.class, InputStream.class, actual); }
private void addLineNumberToErrorMessage(Throwable error) { StackTraceElement testStackTraceElement = getFirstStackTraceElementFromTest(error.getStackTrace()); if (testStackTraceElement != null) { changeErrorMessage(error, buildErrorMessageWithLineNumber(error.getMessage(), testStackTraceElement)); } }
/** * Returns a copy of list of soft assertions collected errors. * @return a copy of list of soft assertions collected errors. */ public List<Throwable> errorsCollected() { return decorateErrorsCollected(proxies.errorsCollected()); }
/** * Modifies collected errors. Override to customize modification. * @param errors list of errors to decorate * @return decorated list */ protected List<Throwable> decorateErrorsCollected(List<Throwable> errors) { return addLineNumberToErrorMessages(errors); }
/** * Creates a new instance of <code>{@link ShortAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public ShortAssert assertThat(short actual) { return proxy(ShortAssert.class, Short.class, actual); }
/** * Creates a new instance of <code>{@link DateAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public DateAssert assertThat(Date actual) { return proxy(DateAssert.class, Date.class, actual); }
/** * Creates a new instance of <code>{@link BooleanAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public BooleanAssert assertThat(boolean actual) { return proxy(BooleanAssert.class, Boolean.class, actual); }
/** * Creates a new instance of <code>{@link CharacterAssert}</code>. * * @param actual the actual value. * @return the created assertion object. */ public CharacterAssert assertThat(Character actual) { return proxy(CharacterAssert.class, Character.class, actual); }