Class Throwables


  • public class Throwables
    extends Object
    Reusable assertions for Throwables.
    Author:
    Joel Costigliola, Libor Ondrusek, Jack Gough, Mike Gilchrist
    • Constructor Detail

      • Throwables

        Throwables()
    • Method Detail

      • instance

        public static Throwables instance()
        Returns the singleton instance of this class.
        Returns:
        the singleton instance of this class.
      • assertHasMessage

        public void assertHasMessage​(AssertionInfo info,
                                     Throwable actual,
                                     String expectedMessage)
        Asserts that the given actual Throwable message is equal to the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        expectedMessage - the expected message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable is not equal to the given one.
      • assertHasCauseReference

        public void assertHasCauseReference​(AssertionInfo info,
                                            Throwable actual,
                                            Throwable expectedCause)
        Asserts that the actual Throwable has a cause that refers to the expected one.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        expectedCause - the expected cause.
      • assertHasRootCause

        public void assertHasRootCause​(AssertionInfo info,
                                       Throwable actual,
                                       Throwable expectedRootCause)
        Asserts that the actual Throwable has a root cause similar to the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        expectedRootCause - the expected root cause.
      • assertHasNoCause

        public void assertHasNoCause​(AssertionInfo info,
                                     Throwable actual)
        Asserts that the actual Throwable does not have a cause.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the actual Throwable has a cause.
      • assertHasMessageStartingWith

        public void assertHasMessageStartingWith​(AssertionInfo info,
                                                 Throwable actual,
                                                 String description)
        Asserts that the message of the actual Throwable starts with the given description.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        description - the description expected to start the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not start with the given description.
      • assertHasMessageContaining

        public void assertHasMessageContaining​(AssertionInfo info,
                                               Throwable actual,
                                               String description)
        Asserts that the message of the actual Throwable contains with the given description.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        description - the description expected to be contained in the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not contain the given description.
      • assertHasMessageContainingAll

        public void assertHasMessageContainingAll​(AssertionInfo info,
                                                  Throwable actual,
                                                  CharSequence... values)
        Asserts that the message of the actual Throwable contains with the given values.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        values - the Strings expected to be contained in the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not contain the given description.
      • assertHasMessageNotContaining

        public void assertHasMessageNotContaining​(AssertionInfo info,
                                                  Throwable actual,
                                                  String content)
        Asserts that the message of the actual Throwable does not contain the given content or is null.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        content - the content expected not to be contained in the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable contains the given content.
        Since:
        3.12.0
      • assertHasMessageNotContainingAny

        public void assertHasMessageNotContainingAny​(AssertionInfo info,
                                                     Throwable actual,
                                                     CharSequence... values)
        Asserts that the message of the actual Throwable does not contain any of the given values or is null.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        values - the contents expected to not be contained in the actual Throwables's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not contain the given description.
      • assertHasStackTraceContaining

        public void assertHasStackTraceContaining​(AssertionInfo info,
                                                  Throwable actual,
                                                  String description)
        Asserts that the stack trace of the actual Throwable contains with the given description.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        description - the description expected to be contained in the actual Throwable's stack trace.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the stack trace of the actual Throwable does not contain the given description.
      • assertHasMessageMatching

        public void assertHasMessageMatching​(AssertionInfo info,
                                             Throwable actual,
                                             String regex)
        Asserts that the message of the actual Throwable matches with the given regular expression.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        regex - the regular expression of value expected to be matched the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not match the given regular expression.
        NullPointerException - if the regex is null
      • assertHasMessageFindingMatch

        public void assertHasMessageFindingMatch​(AssertionInfo info,
                                                 Throwable actual,
                                                 String regex)
        Asserts that a sequence of the message of the actual Throwable matches with the given regular expression (see Matcher.find()). The Pattern used under the hood enables the Pattern.DOTALL mode.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        regex - the regular expression expected to be found in the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable doesn't contain any sequence matching with the given regular expression
        NullPointerException - if the regex is null
      • assertHasMessageEndingWith

        public void assertHasMessageEndingWith​(AssertionInfo info,
                                               Throwable actual,
                                               String description)
        Asserts that the message of the actual Throwable ends with the given description.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        description - the description expected to end the actual Throwable's message.
        Throws:
        AssertionError - if the actual Throwable is null.
        AssertionError - if the message of the actual Throwable does not end with the given description.
      • assertHasCauseInstanceOf

        public void assertHasCauseInstanceOf​(AssertionInfo info,
                                             Throwable actual,
                                             Class<? extends Throwable> type)
        Assert that the cause of actual Throwable is an instance of the given type.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        type - the expected cause type.
        Throws:
        NullPointerException - if given type is null.
        AssertionError - if the actual Throwable is null.
        AssertionError - if the actual Throwable has no cause.
        AssertionError - if the cause of the actual Throwable is not an instance of the given type.
      • assertHasCauseExactlyInstanceOf

        public void assertHasCauseExactlyInstanceOf​(AssertionInfo info,
                                                    Throwable actual,
                                                    Class<? extends Throwable> type)
        Assert that the cause of actual Throwable is exactly an instance of the given type.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        type - the expected cause type.
        Throws:
        NullPointerException - if given type is null.
        AssertionError - if the actual Throwable is null.
        AssertionError - if the actual Throwable has no cause.
        AssertionError - if the cause of the actual Throwable is not exactly an instance of the given type.
      • assertHasRootCauseInstanceOf

        public void assertHasRootCauseInstanceOf​(AssertionInfo info,
                                                 Throwable actual,
                                                 Class<? extends Throwable> type)
        Assert that the root cause of actual Throwable is an instance of the given type.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        type - the expected cause type.
        Throws:
        NullPointerException - if given type is null.
        AssertionError - if the actual Throwable is null.
        AssertionError - if the actual Throwable has no cause.
        AssertionError - if the cause of the actual Throwable is not an instance of the given type.
      • assertHasRootCauseExactlyInstanceOf

        public void assertHasRootCauseExactlyInstanceOf​(AssertionInfo info,
                                                        Throwable actual,
                                                        Class<? extends Throwable> type)
        Assert that the root cause of actual Throwable is exactly an instance of the given type.
        Parameters:
        info - contains information about the assertion.
        actual - the given Throwable.
        type - the expected cause type.
        Throws:
        NullPointerException - if given type is null.
        AssertionError - if the actual Throwable is null.
        AssertionError - if the actual Throwable has no cause.
        AssertionError - if the root cause of the actual Throwable is not exactly an instance of the given type.
      • assertHasNoSuppressedExceptions

        public void assertHasNoSuppressedExceptions​(AssertionInfo info,
                                                    Throwable actual)
      • assertHasSuppressedException

        public void assertHasSuppressedException​(AssertionInfo info,
                                                 Throwable actual,
                                                 Throwable expectedSuppressedException)
      • checkIsNotNull

        private static void checkIsNotNull​(CharSequence... values)
      • checkIsNotEmpty

        private static void checkIsNotEmpty​(CharSequence... values)
      • checkCharSequenceArrayDoesNotHaveNullElements

        private static void checkCharSequenceArrayDoesNotHaveNullElements​(CharSequence[] values)
      • checkCharSequenceIsNotNull

        private static void checkCharSequenceIsNotNull​(CharSequence sequence)
      • compareThrowable

        private static boolean compareThrowable​(Throwable actual,
                                                Throwable expected)