Class ThrowableAssertAlternative<T extends Throwable>

java.lang.Object
org.assertj.core.api.AbstractAssert<ThrowableAssertAlternative<T>,T>
org.assertj.core.api.ThrowableAssertAlternative<T>
All Implemented Interfaces:
Assert<ThrowableAssertAlternative<T>,T>, Descriptable<ThrowableAssertAlternative<T>>, ExtensionPoints<ThrowableAssertAlternative<T>,T>

public class ThrowableAssertAlternative<T extends Throwable> extends AbstractAssert<ThrowableAssertAlternative<T>,T>
Assertion methods for Throwable similar to ThrowableAssert but with assertions methods named differently to make testing code fluent (ex : withMessage instead of hasMessage.

 assertThatExceptionOfType(IOException.class)
           .isThrownBy(() -> { throw new IOException("boom! tcha!"); });
           .withMessage("boom! %s", "tcha!"); 

This class is linked with the ThrowableTypeAssert and allow to check that an exception type is thrown by a lambda.

  • Field Details

  • Constructor Details

    • ThrowableAssertAlternative

      ThrowableAssertAlternative(T actual)
  • Method Details

    • withMessage

      public ThrowableAssertAlternative<T> withMessage(String message)
      Verifies that the message of the actual Throwable is equal to the given one.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessage("wrong amount 123");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessage("wrong amount 123 euros");
      Parameters:
      message - the expected message.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the message of the actual Throwable is not equal to the given one.
      See Also:
    • withMessage

      public ThrowableAssertAlternative<T> withMessage(String message, Object... parameters)
      Verifies that the message of the actual Throwable is equal to the given one built using String.format(String, Object...) syntax.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessage("wrong amount %s, "123");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessage("wrong amount 123 euros");
      Parameters:
      message - a format string representing the expected message
      parameters - argument referenced by the format specifiers in the format string
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the message of the actual Throwable is not equal to the given one.
      See Also:
    • withCause

      public ThrowableAssertAlternative<T> withCause(Throwable cause)
      Verifies that the actual Throwable has a cause similar to the given one, that is with same type and message (it does not use equals method for comparison).

      Example:

       Throwable illegalArgumentException = new IllegalArgumentException("invalid arg");
       Throwable wrappingException = new Throwable(illegalArgumentException);
      
       // This assertion succeeds:
       
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw wrappingException;})
                 .withCause(illegalArgumentException);
      
       // These assertions fail:
       
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw wrappingException;})
                 .withCause(new IllegalArgumentException("bad arg"));
                 
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw wrappingException;})
                 .withCause(new NullPointerException());
                 
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw wrappingException;})
                 .withCause(null);
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the actual Throwable has not the given cause.
      See Also:
    • withNoCause

      public ThrowableAssertAlternative<T> withNoCause()
      Verifies that the actual Throwable does not have a cause.

      Example:

       IllegalArgumentException exception = new IllegalArgumentException();
      
       // This assertion succeeds:
       assertThatExceptionOfType(IllegalArgumentException.class)
                 .isThrownBy(() -> {throw exception;})
                 .withNoCause();
      
       // These assertion fails:
       Throwable illegalArgumentException = new Throwable(exception); 
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withNoCause();
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the actual Throwable has a cause.
      See Also:
    • withMessageStartingWith

      public ThrowableAssertAlternative<T> withMessageStartingWith(String description)
      Verifies that the message of the actual Throwable starts with the given description.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageStartingWith("wrong amount");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageStartingWith("right amount");
      Parameters:
      description - the description expected to start the actual Throwable's message.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the message of the actual Throwable does not start with the given description.
      See Also:
    • withMessageContaining

      public ThrowableAssertAlternative<T> withMessageContaining(String description)
      Verifies that the message of the actual Throwable contains with the given description.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageContaining("amount");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageContaining("456");
      Parameters:
      description - the description expected to be contained in the actual Throwable's message.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the message of the actual Throwable does not contain the given description.
      See Also:
    • withStackTraceContaining

      public ThrowableAssertAlternative<T> withStackTraceContaining(String description)
      Verifies that the stack trace of the actual Throwable contains with the given description.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withStackTraceContaining("amount");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withStackTraceContaining("456");
      Parameters:
      description - the description expected to be contained in the actual Throwable's stack trace.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the stack trace of the actual Throwable does not contain the given description.
      See Also:
    • withMessageMatching

      public ThrowableAssertAlternative<T> withMessageMatching(String regex)
      Verifies that the message of the actual Throwable matches with the given regular expression.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageMatching("wrong amount [0-9]*");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageMatching("wrong amount [0-9]* euros");
      Parameters:
      regex - the regular expression of value expected to be matched the actual Throwable's message.
      Returns:
      this assertion object.
      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
      See Also:
    • withMessageEndingWith

      public ThrowableAssertAlternative<T> withMessageEndingWith(String description)
      Verifies that the message of the actual Throwable ends with the given description.

      Examples:

       Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageEndingWith("123");
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw illegalArgumentException;})
                 .withMessageEndingWith("456");
      Parameters:
      description - the description expected to end the actual Throwable's message.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Throwable is null.
      AssertionError - if the message of the actual Throwable does not end with the given description.
      See Also:
    • withCauseInstanceOf

      public ThrowableAssertAlternative<T> withCauseInstanceOf(Class<? extends Throwable> type)
      Verifies that the cause of the actual Throwable is an instance of the given type.

      Example:

       Throwable throwable = new Throwable(new NullPointerException());
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseInstanceOf(NullPointerException.class);
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseInstanceOf(RuntimeException.class);
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseInstanceOf(IllegalArgumentException.class);
      Parameters:
      type - the expected cause type.
      Returns:
      this assertion object.
      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.
      See Also:
    • withCauseExactlyInstanceOf

      public ThrowableAssertAlternative<T> withCauseExactlyInstanceOf(Class<? extends Throwable> type)
      Verifies that the cause of the actual Throwable is exactly an instance of the given type.

      Example:

       Throwable throwable = new Throwable(new NullPointerException());
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseExactlyInstanceOf(NullPointerException.class);
      
       // assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match)
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseExactlyInstanceOf(RuntimeException.class);
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withCauseExactlyInstanceOf(IllegalArgumentException.class);
      Parameters:
      type - the expected cause type.
      Returns:
      this assertion object.
      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.
      See Also:
    • withRootCauseInstanceOf

      public ThrowableAssertAlternative<T> withRootCauseInstanceOf(Class<? extends Throwable> type)
      Verifies that the root cause of the actual Throwable is an instance of the given type.

      Example:

       Throwable throwable = new Throwable(
                                  new IllegalStateException(
                                      new NullPointerException()));
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseInstanceOf(NullPointerException.class);
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseInstanceOf(RuntimeException.class);
      
       // assertion will fail
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseInstanceOf(IllegalStateException.class);
      Parameters:
      type - the expected cause type.
      Returns:
      this assertion object.
      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.
      See Also:
    • withRootCauseExactlyInstanceOf

      public ThrowableAssertAlternative<T> withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)
      Verifies that the root cause of the actual Throwable is exactly an instance of the given type.

      Example:

       Throwable throwable = new Throwable(
                                  new IllegalStateException(
                                      new NullPointerException()));
      
       // assertion will pass
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseExactlyInstanceOf(NullPointerException.class);
      
       // assertion will fail (even if NullPointerException is a RuntimeException since we want an exact match)
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseExactlyInstanceOf(RuntimeException.class);
       assertThatExceptionOfType(Throwable.class)
                 .isThrownBy(() -> {throw throwable;})
                 .withRootCauseExactlyInstanceOf(IllegalStateException.class);
      Parameters:
      type - the expected cause type.
      Returns:
      this assertion object.
      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.
      See Also: