public class AbstractSoftAssertions extends java.lang.Object implements InstanceOfAssertFactories
Modifier and Type | Class and Description |
---|---|
static interface |
AbstractSoftAssertions.ThrowingRunnable |
Modifier and Type | Field and Description |
---|---|
protected SoftProxies |
proxies |
ARRAY, ATOMIC_BOOLEAN, ATOMIC_INTEGER, ATOMIC_INTEGER_ARRAY, ATOMIC_INTEGER_FIELD_UPDATER, ATOMIC_LONG, ATOMIC_LONG_ARRAY, ATOMIC_LONG_FIELD_UPDATER, ATOMIC_MARKABLE_REFERENCE, ATOMIC_REFERENCE, ATOMIC_REFERENCE_ARRAY, ATOMIC_REFERENCE_FIELD_UPDATER, ATOMIC_STAMPED_REFERENCE, BIG_DECIMAL, BIG_INTEGER, BOOLEAN, BOOLEAN_ARRAY, BYTE, BYTE_ARRAY, CHAR_ARRAY, CHAR_SEQUENCE, CHARACTER, CLASS, COMPLETABLE_FUTURE, COMPLETION_STAGE, DATE, DOUBLE, DOUBLE_ARRAY, DOUBLE_PREDICATE, DOUBLE_STREAM, FILE, FLOAT, FLOAT_ARRAY, FUTURE, INPUT_STREAM, INSTANT, INT_ARRAY, INT_PREDICATE, INT_STREAM, INTEGER, ITERABLE, ITERATOR, LIST, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_ARRAY, LONG_PREDICATE, LONG_STREAM, MAP, OFFSET_DATE_TIME, OFFSET_TIME, OPTIONAL, OPTIONAL_DOUBLE, OPTIONAL_INT, OPTIONAL_LONG, PATH, PREDICATE, SHORT, SHORT_ARRAY, STREAM, STRING, STRING_BUFFER, STRING_BUILDER, THROWABLE, URI_TYPE, URL_TYPE, ZONED_DATE_TIME
Constructor and Description |
---|
AbstractSoftAssertions() |
Modifier and Type | Method and Description |
---|---|
private java.lang.Throwable |
addLineNumberToErrorMessage(java.lang.Throwable error) |
private java.util.List<java.lang.Throwable> |
addLineNumberToErrorMessages(java.util.List<java.lang.Throwable> errors) |
private java.lang.String |
buildErrorMessageWithLineNumber(java.lang.String originalErrorMessage,
java.lang.StackTraceElement testStackTraceElement) |
void |
check(AbstractSoftAssertions.ThrowingRunnable assertion)
Catch and collect assertion errors coming from standard and custom assertions.
|
private java.lang.Throwable |
createNewInstanceWithLineNumberInErrorMessage(java.lang.Throwable error,
java.lang.StackTraceElement testStackTraceElement) |
protected java.util.List<java.lang.Throwable> |
decorateErrorsCollected(java.util.List<java.lang.Throwable> errors)
Modifies collected errors.
|
java.util.List<java.lang.Throwable> |
errorsCollected()
Returns a copy of list of soft assertions collected errors.
|
void |
fail(java.lang.String failureMessage)
Fails with the given message.
|
void |
fail(java.lang.String failureMessage,
java.lang.Object... args)
Fails with the given message built like
String.format(String, Object...) . |
void |
fail(java.lang.String failureMessage,
java.lang.Throwable realCause)
Fails with the given message and with the
Throwable that caused the failure. |
void |
failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Fails with a message explaining that a
Throwable of given class was expected to be thrown
but had not been. |
private java.lang.StackTraceElement |
getFirstStackTraceElementFromTest(java.lang.StackTraceElement[] stacktrace) |
private boolean |
isProxiedAssertionClass(java.lang.String className) |
<T,V> V |
proxy(java.lang.Class<V> assertClass,
java.lang.Class<T> actualClass,
T actual) |
void |
shouldHaveThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Fails with a message explaining that a
Throwable of given class was expected to be thrown
but had not been. |
private java.lang.String |
simpleClassNameOf(java.lang.StackTraceElement testStackTraceElement) |
boolean |
wasSuccess()
Returns the result of last soft assertion which can be used to decide what the next one should be.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
array, atomicIntegerFieldUpdater, atomicLongFieldUpdater, atomicMarkableReference, atomicReference, atomicReferenceArray, atomicReferenceFieldUpdater, atomicStampedReference, comparable, completableFuture, completionStage, future, iterable, iterator, list, map, optional, predicate, stream, type
protected final SoftProxies proxies
public <T,V> V proxy(java.lang.Class<V> assertClass, java.lang.Class<T> actualClass, T actual)
public void check(AbstractSoftAssertions.ThrowingRunnable assertion)
Example :
SoftAssertions softly = new SoftAssertions();
softly.check(() -> Assertions.assertThat(…).…);
softly.check(() -> CustomAssertions.assertThat(…).…);
softly.assertAll();
assertion
- an assertion call.public void fail(java.lang.String failureMessage)
failureMessage
- error message.public void fail(java.lang.String failureMessage, java.lang.Object... args)
String.format(String, Object...)
.failureMessage
- error message.args
- Arguments referenced by the format specifiers in the format string.public void fail(java.lang.String failureMessage, java.lang.Throwable realCause)
Throwable
that caused the failure.failureMessage
- error message.realCause
- cause of the error.public void failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Throwable
of given class was expected to be thrown
but had not been.throwableClass
- the Throwable class that was expected to be thrown.java.lang.AssertionError
- with a message explaining that a Throwable
of given class was expected to be thrown but had
not been.Fail.shouldHaveThrown(Class)
can be used as a replacement.public void shouldHaveThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Throwable
of given class was expected to be thrown
but had not been.throwableClass
- the Throwable class that was expected to be thrown.java.lang.AssertionError
- with a message explaining that a Throwable
of given class was expected to be thrown but had
not been.public java.util.List<java.lang.Throwable> errorsCollected()
protected java.util.List<java.lang.Throwable> decorateErrorsCollected(java.util.List<java.lang.Throwable> errors)
errors
- list of errors to decoratepublic boolean wasSuccess()
Example :
Person person = ...
SoftAssertions soft = new SoftAssertions();
if (soft.assertThat(person.getAddress()).isNotNull().wasSuccess()) {
soft.assertThat(person.getAddress().getStreet()).isNotNull();
}
private java.util.List<java.lang.Throwable> addLineNumberToErrorMessages(java.util.List<java.lang.Throwable> errors)
private java.lang.Throwable addLineNumberToErrorMessage(java.lang.Throwable error)
private java.lang.Throwable createNewInstanceWithLineNumberInErrorMessage(java.lang.Throwable error, java.lang.StackTraceElement testStackTraceElement) throws java.lang.ReflectiveOperationException
java.lang.ReflectiveOperationException
private java.lang.String buildErrorMessageWithLineNumber(java.lang.String originalErrorMessage, java.lang.StackTraceElement testStackTraceElement)
private java.lang.String simpleClassNameOf(java.lang.StackTraceElement testStackTraceElement)
private java.lang.StackTraceElement getFirstStackTraceElementFromTest(java.lang.StackTraceElement[] stacktrace)
private boolean isProxiedAssertionClass(java.lang.String className)