com.googlecode.catchexception.throwable.apis
Class BDDCatchThrowable

java.lang.Object
  extended by com.googlecode.catchexception.throwable.apis.BDDCatchThrowable
Direct Known Subclasses:
CatchThrowableAssertJ

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);
 

Since:
1.3.0
Author:
rwoo, mariuszs

Constructor Summary
BDDCatchThrowable()
           
 
Method Summary
static Throwable caughtThrowable()
          Returns the throwable caught during the last call on the proxied object in the current thread.
static org.assertj.core.api.AbstractThrowableAssert<?,? extends Throwable> then(Throwable actualThrowable)
          Deprecated. Use BDDAssertions#then(Throwable) instead
static void thenThrown(Class actualThrowableClazz)
          Throws an assertion if no throwable is thrown or if an throwable of an unexpected type is thrown.
static
<T> T
when(T obj)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BDDCatchThrowable

public BDDCatchThrowable()
Method Detail

when

public static <T> T when(T obj)
Type Parameters:
T - The type of the given obj.
Parameters:
obj - The instance that shall be proxied. Must not be null.
Returns:
Returns a proxy for the given object. The proxy catches throwables of the given type when a method on the proxy is called.
See Also:
CatchThrowable.catchThrowable(Object)

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.

caughtThrowable

public static Throwable caughtThrowable()
Returns the throwable caught during the last call on the proxied object in the current thread.

Returns:
Returns the boxed 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(Object). Returns null if the proxy has not caught an throwable. Returns null if the caught throwable belongs to a class that is no longer loaded.

then

@Deprecated
public static org.assertj.core.api.AbstractThrowableAssert<?,? extends Throwable> then(Throwable actualThrowable)
Deprecated. Use BDDAssertions#then(Throwable) instead

Enables AssertJ assertions about the caught throwable.

EXAMPLE:

// given an empty list
     List myList = new ArrayList();

     // when we try to get first element of the list
     when(myList).get(1);

     // then we expect an IndexOutOfBoundsThrowable
     then(caughtThrowable())
     .isInstanceOf(IndexOutOfBoundsThrowable.class)
     .hasMessage("Index: 1, Size: 0")
     .hasMessageStartingWith("Index: 1")
     .hasMessageEndingWith("Size: 0")
     .hasMessageContaining("Size")
     .hasNoCause();
     

Parameters:
actualThrowable - the value to be the target of the assertions methods.
Returns:
Returns the created assertion object.


Copyright © 2015. All rights reserved.