Class BDDCatchThrowable

java.lang.Object
com.googlecode.catchexception.throwable.apis.BDDCatchThrowable

public class BDDCatchThrowable extends Object
Supports BDD-like approach to catch and verify throwables (given/when/then).

import static com.googlecode.catchexception.throwable.apis .BDDCatchThrowable.*; // given an empty list List myList = new ArrayList(); // when we try to get the first element of the list when(myList).get(1); // then we expect an IndexOutOfBoundsThrowable then(caughtThrowable()) .isInstanceOf(IndexOutOfBoundsThrowable.class) .hasMessage("Index: 1, Size: 0") .hasNoCause(); // then we expect an IndexOutOfBoundsThrowable (alternatively) thenThrown(IndexOutOfBoundsThrowable.class);

  • Constructor Details

    • BDDCatchThrowable

      public BDDCatchThrowable()
  • Method Details

    • when

      public static void when(ThrowingCallable actor)
      When.
      Parameters:
      actor - The instance that shall be proxied. Must not be null.
      See Also:
    • caughtThrowable

      public static Throwable caughtThrowable()
      Returns the throwable caught during the last call on the proxied object in the current thread.
      Returns:
      Returns the throwable caught during the last call on the proxied object in the current thread - if the call was made through a proxy that has been created via when(ThrowingCallable). Returns null the proxy has not caught an throwable. Returns null if the caught throwable belongs to a class that is no longer loaded.
    • caughtThrowable

      @Deprecated(since="2.3.0", forRemoval=true) public static <T extends Throwable> T caughtThrowable(Class<T> caughtThrowableType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use caghtThrowable() instead as the passed argument does nothing
      Caught throwable.
      Type Parameters:
      T - the generic type
      Parameters:
      caughtThrowableType - the caught throwable type
      Returns:
      the t
    • thenThrown

      public static void thenThrown(Class actualThrowableClazz)
      Throws an assertion if no throwable is thrown or if an throwable of an unexpected type is thrown.

      EXAMPLE: // given a list with nine members List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); // when we try to get the 500th member of the fellowship when(myList).get(500); // then we expect an IndexOutOfBoundsThrowable thenThrown(IndexOutOfBoundsThrowable.class);

      Parameters:
      actualThrowableClazz - the expected type of the caught throwable.