Class AbstractSoftAssertions

    • Constructor Detail

      • AbstractSoftAssertions

        public AbstractSoftAssertions()
    • Method Detail

      • proxy

        public <T,​V> V proxy​(Class<V> assertClass,
                                   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​(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 List<Throwable> decorateErrorsCollected​(List<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.
      • addLineNumberToErrorMessage

        private Throwable addLineNumberToErrorMessage​(Throwable error)
      • buildErrorMessageWithLineNumber

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

        private boolean isProxiedAssertionClass​(String className)