Class AbstractSoftAssertions

    • Constructor Detail

      • AbstractSoftAssertions

        public AbstractSoftAssertions()
    • Method Detail

      • 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: 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 them using collectAssertionError().
        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​(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 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​(java.lang.String failureMessage,
                         java.lang.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​(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.
        Parameters:
        throwableClass - the Throwable class that was expected to be thrown.
        Throws:
        java.lang.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​(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.
        Parameters:
        throwableClass - the Throwable class that was expected to be thrown.
        Throws:
        java.lang.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
      • assertionErrorsCollected

        public java.util.List<java.lang.AssertionError> assertionErrorsCollected()
        Returns a copy of list of soft assertions collected errors.
        Specified by:
        assertionErrorsCollected in interface AssertionErrorCollector
        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:
        wasSuccess in interface SoftAssertionsProvider
        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)