Package org.assertj.core.api
Class AbstractSoftAssertions
- java.lang.Object
-
- org.assertj.core.api.AbstractSoftAssertions
-
- All Implemented Interfaces:
AfterAssertionErrorCollected,AssertionErrorCollector,InstanceOfAssertFactories,SoftAssertionsProvider
- Direct Known Subclasses:
BDDSoftAssertions,Java6BDDSoftAssertions,Java6JUnitBDDSoftAssertions,Java6JUnitSoftAssertions,Java6SoftAssertions,JUnitBDDSoftAssertions,JUnitJupiterBDDSoftAssertions,JUnitJupiterSoftAssertions,JUnitSoftAssertions,SoftAssertions
public abstract class AbstractSoftAssertions extends java.lang.Object implements SoftAssertionsProvider, InstanceOfAssertFactories
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.assertj.core.api.SoftAssertionsProvider
SoftAssertionsProvider.ThrowingRunnable
-
-
Field Summary
Fields Modifier and Type Field Description private AssertionErrorCreatorassertionErrorCreatorprotected SoftProxiesproxies-
Fields inherited from interface org.assertj.core.api.InstanceOfAssertFactories
ARRAY, ARRAY_2D, 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_2D_ARRAY, BOOLEAN_ARRAY, BYTE, BYTE_2D_ARRAY, BYTE_ARRAY, CHAR_2D_ARRAY, CHAR_ARRAY, CHAR_SEQUENCE, CHARACTER, CLASS, COMPLETABLE_FUTURE, COMPLETION_STAGE, DATE, DOUBLE, DOUBLE_2D_ARRAY, DOUBLE_ARRAY, DOUBLE_PREDICATE, DOUBLE_STREAM, DURATION, FILE, FLOAT, FLOAT_2D_ARRAY, FLOAT_ARRAY, FUTURE, INPUT_STREAM, INSTANT, INT_2D_ARRAY, INT_ARRAY, INT_PREDICATE, INT_STREAM, INTEGER, ITERABLE, ITERATOR, LIST, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_2D_ARRAY, LONG_ADDER, LONG_ARRAY, LONG_PREDICATE, LONG_STREAM, MAP, OFFSET_DATE_TIME, OFFSET_TIME, OPTIONAL, OPTIONAL_DOUBLE, OPTIONAL_INT, OPTIONAL_LONG, PATH, PERIOD, PREDICATE, SHORT, SHORT_2D_ARRAY, SHORT_ARRAY, SPLITERATOR, 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 <T extends java.lang.Throwable>
TaddLineNumberToErrorMessage(T error)private <T extends java.lang.Throwable>
java.util.List<T>addLineNumberToErrorMessages(java.util.List<? extends T> errors)voidassertAll()Verifies that no soft assertions have failed.java.util.List<java.lang.AssertionError>assertionErrorsCollected()Returns a copy of list of soft assertions collected errors.private java.lang.StringbuildErrorMessageWithLineNumber(java.lang.String originalErrorMessage, java.lang.StackTraceElement testStackTraceElement)voidcollectAssertionError(java.lang.AssertionError error)This method can be used to collect soft assertion errors.private <T extends java.lang.Throwable>
TcreateNewInstanceWithLineNumberInErrorMessage(T error, java.lang.StackTraceElement testStackTraceElement)protected <T extends java.lang.Throwable>
java.util.List<T>decorateErrorsCollected(java.util.List<? extends T> errors)Modifies collected errors.java.util.List<java.lang.Throwable>errorsCollected()Returns a copy of list of soft assertions collected errors.voidfail(java.lang.String failureMessage)Fails with the given message.voidfail(java.lang.String failureMessage, java.lang.Object... args)Fails with the given message built likeString.format(String, Object...).voidfail(java.lang.String failureMessage, java.lang.Throwable realCause)Fails with the given message and with theThrowablethat caused the failure.voidfailBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)Fails with a message explaining that aThrowableof given class was expected to be thrown but had not been.private java.lang.StackTraceElementgetFirstStackTraceElementFromTest(java.lang.StackTraceElement[] stacktrace)private booleanisProxiedAssertionClass(java.lang.String className)<SELF extends Assert<? extends SELF,? extends ACTUAL>,ACTUAL>
SELFproxy(java.lang.Class<SELF> assertClass, java.lang.Class<ACTUAL> actualClass, ACTUAL actual)Creates a proxied assertion class of the given type.voidsetAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)Register a callback allowing to react after anAssertionErroris collected by the current soft assertion.voidshouldHaveThrown(java.lang.Class<? extends java.lang.Throwable> throwableClass)Fails with a message explaining that aThrowableof given class was expected to be thrown but had not been.private java.lang.StringsimpleClassNameOf(java.lang.StackTraceElement testStackTraceElement)booleanwasSuccess()Returns the result of last soft assertion which can be used to decide what the next one should be.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.AssertionErrorCollector
onAssertionErrorCollected
-
Methods inherited from interface org.assertj.core.api.SoftAssertionsProvider
assertAlso, check
-
-
-
-
Field Detail
-
proxies
protected final SoftProxies proxies
-
assertionErrorCreator
private final AssertionErrorCreator assertionErrorCreator
-
-
Method Detail
-
setAfterAssertionErrorCollected
public void setAfterAssertionErrorCollected(AfterAssertionErrorCollected afterAssertionErrorCollected)
Register a callback allowing to react after anAssertionErroris collected by the current soft assertion.The callback is an instance of
AfterAssertionErrorCollectedwhich can be expressed as lambda.Example:
SoftAssertions softly = new SoftAssertions(); StringBuilder reportBuilder = new StringBuilder(format("Assertions report:%n")); // register our callback softly.setAfterAssertionErrorCollected(error -> reportBuilder.append(String.format("------------------%n%s%n", error.getMessage()))); // the AssertionError corresponding to the failing assertions are registered in the report softly.assertThat("The Beatles").isEqualTo("The Rolling Stones"); softly.assertThat(123).isEqualTo(123) .isEqualTo(456);resulting
reportBuilder:Assertions report: ------------------ Expecting: <"The Beatles"> to be equal to: <"The Rolling Stones"> but was not. ------------------ Expecting: <123> to be equal to: <456> but was not.Alternatively, if you have defined your own SoftAssertions subclass and inherited from
AbstractSoftAssertions, the only thing you have to do is to overrideAfterAssertionErrorCollected.onAssertionErrorCollected(AssertionError).- Parameters:
afterAssertionErrorCollected- the callback.- Since:
- 3.17.0
-
assertAll
public void assertAll()
Description copied from interface:SoftAssertionsProviderVerifies that no soft assertions have failed.- Specified by:
assertAllin interfaceSoftAssertionsProvider
-
proxy
public <SELF extends Assert<? extends SELF,? extends ACTUAL>,ACTUAL> SELF proxy(java.lang.Class<SELF> assertClass, java.lang.Class<ACTUAL> actualClass, ACTUAL actual)
Description copied from interface:SoftAssertionsProviderCreates a proxied assertion class of the given type. The returned value is an assertion object compatible with the supplied assertion class, but instead of throwing errors it will collect them and store.- Specified by:
proxyin interfaceSoftAssertionsProvider- Type Parameters:
SELF- The type of the assertion classACTUAL- The type of the object-under-test- Parameters:
assertClass- Class instance for the assertion type.actualClass- Class instance for the type of the object-under-test.actual- The actual object-under-test.- Returns:
- A proxied assertion class for the given object-under-test.
-
collectAssertionError
public void collectAssertionError(java.lang.AssertionError error)
Description copied from interface:AssertionErrorCollectorThis method can be used to collect soft assertion errors.Warning: this is not the method used internally by AssertJ to collect all of them, overriding it to react to each collected assertion error will not work.
To be able to react after an assertion error is collected, use @
AssertionErrorCollector.onAssertionErrorCollected(AssertionError)instead.- Specified by:
collectAssertionErrorin interfaceAssertionErrorCollector- Parameters:
error- theAssertionErrorto collect.
-
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 theThrowablethat 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 aThrowableof 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 aThrowableof 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 aThrowableof 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 aThrowableof given class was expected to be thrown but had not been.- Since:
- 2.6.0 / 3.6.0
-
assertionErrorsCollected
public java.util.List<java.lang.AssertionError> assertionErrorsCollected()
Returns a copy of list of soft assertions collected errors.- Specified by:
assertionErrorsCollectedin interfaceAssertionErrorCollector- Returns:
- a copy of list of soft assertions collected errors.
-
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 <T extends java.lang.Throwable> java.util.List<T> decorateErrorsCollected(java.util.List<? extends T> errors)
Modifies collected errors. Override to customize modification.- Type Parameters:
T- the supertype to use in the list return value- 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(); }- Specified by:
wasSuccessin interfaceSoftAssertionsProvider- Returns:
- true if the last assertion was a success.
-
addLineNumberToErrorMessages
private <T extends java.lang.Throwable> java.util.List<T> addLineNumberToErrorMessages(java.util.List<? extends T> errors)
-
addLineNumberToErrorMessage
private <T extends java.lang.Throwable> T addLineNumberToErrorMessage(T error)
-
createNewInstanceWithLineNumberInErrorMessage
private <T extends java.lang.Throwable> T createNewInstanceWithLineNumberInErrorMessage(T 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)
-
-