Class Assertion


  • public final class Assertion
    extends Object
    Assertions help us to build better code, with more robustness. Assertions have been introduced by B.Meyer in a language called Eiffel. An assertion allows you to design by contract. Each time an assertion fails, an specific exception is thrown. The following assertions help you to test if - an object is noT null => isNotNull - a string is not blank (and not null) =>isNotBlank - an expression is true or false =>isTrue or isFalse You can have a condition before running an assertion => when That's usefull when you want to test a pattern of an object that can be null. This assertion should be written in a fluent style to group all the assertions into a single block of code. Assertion can define a message and args. "hello {0}, an error occured on '{1}'", "foo", "bar" returns hello foo, an error occured on 'bar' You can use the simple quote ' inside the message.
    Author:
    pchretien, fconstantin
    • Method Detail

      • when

        public Assertion when​(boolean condition,
                              Supplier<Assertion> assertionSupplier)
        Evaluates an assertion when a condition is fulfilled.
        Parameters:
        condition - the condition to check the assertion
        assertionSupplier - the assertion to check when the condition is fulfilled
        Returns:
        the current assertion
      • isNotNull

        public Assertion isNotNull​(Object o)
        Checks if an object is not null. Throws the famous NullPointerException if not.
        Parameters:
        o - the object
        Returns:
        the current assertion
      • isNotNull

        public Assertion isNotNull​(Object o,
                                   String msg,
                                   Object... params)
        Checks if an object is not null. Throws the famous NullPointerException with a pretty message if not.
        Parameters:
        o - the object
        msg - the message
        params - the params of the message
        Returns:
        the current assertion
      • isNull

        public Assertion isNull​(Object o)
        Checks if an object is null. Throws an IllegalStateException if not
        Parameters:
        o - the object
        Returns:
        the current assertion
      • isNull

        public Assertion isNull​(Object o,
                                String msg,
                                Object... params)
        Checks if an object is null. Throws an illegalStateException with a pretty message if not.
        Parameters:
        o - the object
        msg - the message
        params - the params of the message
        Returns:
        the current assertion
      • isTrue

        public Assertion isTrue​(boolean test,
                                String msg,
                                Object... params)
        Checks if a boolean expression is True Throws an IllegalStateException with a pretty message if not.
        Parameters:
        test - the boolean expression
        msg - the message
        params - the params of the message
        Returns:
        the current assertion
      • isFalse

        public Assertion isFalse​(boolean test,
                                 String msg,
                                 Object... params)
        Checks if a boolean expression is False Throws an IllegalStateException with a pretty message if not.
        Parameters:
        test - the boolean expression
        msg - the message
        params - the params of the message
        Returns:
        the current assertion
      • isNotBlank

        public Assertion isNotBlank​(String str)
        Checks if a string is not blank (and not empty). Throws an IllegalArgumentException if not.
        Parameters:
        str - the string
        Returns:
        the current assertion
      • isNotBlank

        public Assertion isNotBlank​(String str,
                                    String msg,
                                    Object... params)
        Checks if a string is not blank (and not empty). Throws an IllegalArgumentException with a pretty message if not.
        Parameters:
        str - the string
        msg - the message
        params - the params of the message
        Returns:
        the current assertion
      • isValid

        public Assertion isValid​(Supplier<Assertion> assertionSupplier)
        Checks if an assertion supplied is valid. Throws the Exception thrown by the assertion if not.
        Parameters:
        assertionSupplier - the assertion supplied
        Returns:
        the current assertion