Class AbstractSoftAssertions

    • Constructor Detail

      • AbstractSoftAssertions

        public AbstractSoftAssertions()
    • Method Detail

      • setAfterAssertionErrorCollected

        public void setAfterAssertionErrorCollected​(AfterAssertionErrorCollected afterAssertionErrorCollected)
        Register a callback allowing to react after an AssertionError is collected by the current soft assertion.

        The callback is an instance of AfterAssertionErrorCollected which 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 override AfterAssertionErrorCollected.onAssertionErrorCollected(AssertionError).

        Parameters:
        afterAssertionErrorCollected - the callback.
        Since:
        3.17.0
      • proxy

        public <SELF extends Assert<? extends SELF,​? extends ACTUAL>,​ACTUAL> SELF proxy​(Class<SELF> assertClass,
                                                                                                    Class<ACTUAL> actualClass,
                                                                                                    ACTUAL actual)
        Description copied from interface: SoftAssertionsProvider
        Creates 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:
        proxy in interface SoftAssertionsProvider
        Type Parameters:
        SELF - The type of the assertion class
        ACTUAL - 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.
      • fail

        public void fail​(String failureMessage)
        Fails with the given message.
        Parameters:
        failureMessage - error message.
        Since:
        2.6.0 / 3.6.0
      • fail

        public void fail​(String failureMessage,
                         Object... args)
        Fails with the given message built like String.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​(String failureMessage,
                         Throwable realCause)
        Fails with the given message and with the Throwable that caused the failure.
        Parameters:
        failureMessage - error message.
        realCause - cause of the error.
        Since:
        2.6.0 / 3.6.0
      • failBecauseExceptionWasNotThrown

        public void failBecauseExceptionWasNotThrown​(Class<? extends Throwable> throwableClass)
        Fails with a message explaining that a Throwable of given class was expected to be thrown but had not been.
        Parameters:
        throwableClass - the Throwable class that was expected to be thrown.
        Throws:
        AssertionError - with a message explaining that a Throwable 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​(Class<? extends Throwable> throwableClass)
        Fails with a message explaining that a Throwable of given class was expected to be thrown but had not been.
        Parameters:
        throwableClass - the Throwable class that was expected to be thrown.
        Throws:
        AssertionError - with a message explaining that a Throwable of given class was expected to be thrown but had not been.
        Since:
        2.6.0 / 3.6.0
      • errorsCollected

        public List<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 ThrowableList<T> decorateErrorsCollected​(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:
        wasSuccess in interface SoftAssertionsProvider
        Returns:
        true if the last assertion was a success.
      • addLineNumberToErrorMessages

        private <T extends ThrowableList<T> addLineNumberToErrorMessages​(List<? extends T> errors)
      • addLineNumberToErrorMessage

        private <T extends Throwable> T addLineNumberToErrorMessage​(T error)
      • buildErrorMessageWithLineNumber

        private String buildErrorMessageWithLineNumber​(String originalErrorMessage,
                                                       StackTraceElement testStackTraceElement)
      • isProxiedAssertionClass

        private boolean isProxiedAssertionClass​(String className)