Class DefaultAssertionErrorCollector

    • Constructor Detail

      • DefaultAssertionErrorCollector

        public DefaultAssertionErrorCollector()
    • Method Detail

      • setDelegate

        public void setDelegate​(AssertionErrorCollector delegate)
        Description copied from interface: AssertionErrorCollector
        Optionally sets a "delegate" collector into which the collected assertions will be deposited.

        Note that if you set a delegate, this instance will no longer collect or report assertion errors itself but will forward them all to the delegate for collection.

        Specified by:
        setDelegate in interface AssertionErrorCollector
        Parameters:
        delegate - the AssertionErrorCollector to which the assertions will be forwarded.
      • assertionErrorsCollected

        public List<AssertionError> assertionErrorsCollected()
        Returns a list of soft assertions collected errors. If a delegate has been set (see setDelegate(), then this method will return the result of the delegate's assertErrorsCollected().
        Specified by:
        assertionErrorsCollected in interface AssertionErrorCollector
        Returns:
        A list of soft assertions collected errors.
      • 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
      • wasSuccess

        public boolean wasSuccess()
        Description copied from interface: AssertionErrorCollector
        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 AssertionErrorCollector
        Returns:
        true if the last assertion was a success.
      • 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
      • addLineNumberToErrorMessages

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

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

        private static <T extends Throwable> boolean isOpentest4jAssertionFailedError​(T error)
      • buildErrorMessageWithLineNumber

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

        private static boolean isProxiedAssertionClass​(String className)