Package org.assertj.core.api
Class AbstractSoftAssertions
- java.lang.Object
-
- org.assertj.core.api.AbstractSoftAssertions
-
- All Implemented Interfaces:
InstanceOfAssertFactories
- Direct Known Subclasses:
Java6AbstractBDDSoftAssertions
,Java6AbstractStandardSoftAssertions
public class AbstractSoftAssertions extends java.lang.Object implements InstanceOfAssertFactories
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
AbstractSoftAssertions.ThrowingRunnable
-
Field Summary
Fields Modifier and Type Field Description protected SoftProxies
proxies
-
Fields inherited from interface org.assertj.core.api.InstanceOfAssertFactories
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 Summary
Constructors Constructor Description AbstractSoftAssertions()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 likeString.format(String, Object...)
.void
fail(java.lang.String failureMessage, java.lang.Throwable realCause)
Fails with the given message and with theThrowable
that caused the failure.void
failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Fails with a message explaining that aThrowable
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>
Vproxy(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 aThrowable
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.
-
-
-
Field Detail
-
proxies
protected final SoftProxies proxies
-
-
Method Detail
-
proxy
public <T,V> V proxy(java.lang.Class<V> assertClass, java.lang.Class<T> actualClass, T actual)
-
check
public void check(AbstractSoftAssertions.ThrowingRunnable assertion)
Catch and collect assertion errors coming from standard and custom assertions.Example :
SoftAssertions softly = new SoftAssertions(); softly.check(() -> Assertions.assertThat(…).…); softly.check(() -> CustomAssertions.assertThat(…).…); softly.assertAll();
- Parameters:
assertion
- an assertion call.
-
fail
public void fail(java.lang.String failureMessage)
Fails with the given message.- Parameters:
failureMessage
- error message.- Since:
- 2.6.0 / 3.6.0
-
fail
public void fail(java.lang.String failureMessage, java.lang.Object... args)
Fails with the given message built likeString.format(String, Object...)
.- Parameters:
failureMessage
- error message.args
- Arguments referenced by the format specifiers in the format string.- Since:
- 2.6.0 / 3.6.0
-
fail
public void fail(java.lang.String failureMessage, java.lang.Throwable realCause)
Fails with the given message and with theThrowable
that caused the failure.- Parameters:
failureMessage
- error message.realCause
- cause of the error.- Since:
- 2.6.0 / 3.6.0
-
failBecauseExceptionWasNotThrown
public void failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Fails with a message explaining that aThrowable
of given class was expected to be thrown but had not been.- Parameters:
throwableClass
- the Throwable class that was expected to be thrown.- Throws:
java.lang.AssertionError
- with a message explaining that aThrowable
of given class was expected to be thrown but had not been.- Since:
- 2.6.0 / 3.6.0
Fail.shouldHaveThrown(Class)
can be used as a replacement.
-
shouldHaveThrown
public void shouldHaveThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)
Fails with a message explaining that aThrowable
of given class was expected to be thrown but had not been.- Parameters:
throwableClass
- the Throwable class that was expected to be thrown.- Throws:
java.lang.AssertionError
- with a message explaining that aThrowable
of given class was expected to be thrown but had not been.- Since:
- 2.6.0 / 3.6.0
-
errorsCollected
public java.util.List<java.lang.Throwable> errorsCollected()
Returns a copy of list of soft assertions collected errors.- Returns:
- a copy of list of soft assertions collected errors.
-
decorateErrorsCollected
protected java.util.List<java.lang.Throwable> decorateErrorsCollected(java.util.List<java.lang.Throwable> errors)
Modifies collected errors. Override to customize modification.- Parameters:
errors
- list of errors to decorate- Returns:
- decorated list
-
wasSuccess
public boolean wasSuccess()
Returns the result of last soft assertion which can be used to decide what the next one should be.Example :
Person person = ... SoftAssertions soft = new SoftAssertions(); if (soft.assertThat(person.getAddress()).isNotNull().wasSuccess()) { soft.assertThat(person.getAddress().getStreet()).isNotNull(); }
- Returns:
- true if the last assertion was a success.
-
addLineNumberToErrorMessages
private java.util.List<java.lang.Throwable> addLineNumberToErrorMessages(java.util.List<java.lang.Throwable> errors)
-
addLineNumberToErrorMessage
private java.lang.Throwable addLineNumberToErrorMessage(java.lang.Throwable error)
-
createNewInstanceWithLineNumberInErrorMessage
private java.lang.Throwable createNewInstanceWithLineNumberInErrorMessage(java.lang.Throwable error, java.lang.StackTraceElement testStackTraceElement) throws java.lang.ReflectiveOperationException
- Throws:
java.lang.ReflectiveOperationException
-
buildErrorMessageWithLineNumber
private java.lang.String buildErrorMessageWithLineNumber(java.lang.String originalErrorMessage, java.lang.StackTraceElement testStackTraceElement)
-
simpleClassNameOf
private java.lang.String simpleClassNameOf(java.lang.StackTraceElement testStackTraceElement)
-
getFirstStackTraceElementFromTest
private java.lang.StackTraceElement getFirstStackTraceElementFromTest(java.lang.StackTraceElement[] stacktrace)
-
isProxiedAssertionClass
private boolean isProxiedAssertionClass(java.lang.String className)
-
-