public class BDDCatchThrowable extends Object
import static org.assertj.core.api.BDDAssertions.then;
// 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);
The Method org.assertj.core.api.BDDAssertions#then(Throwable)
is originated from AssertJ. You can also use method assertThat
:
// import static org.assertj.core.api.Assertions.assertThat;
// then we expect an IndexOutOfBoundsThrowable
assertThat(caughtThrowable())
.isInstanceOf(IndexOutOfBoundsThrowable.class)
.hasMessage("Index: 1, Size: 0")
.hasMessageStartingWith("Index: 1")
.hasMessageEndingWith("Size: 0")
.hasMessageContaining("Size")
.hasNoCause();
Constructor and Description |
---|
BDDCatchThrowable() |
Modifier and Type | Method and Description |
---|---|
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) |
public BDDCatchThrowable()
public static <T> T when(T obj)
T
- The type of the given obj
.obj
- The instance that shall be proxied. Must not be null
.CatchThrowable.catchThrowable(Object)
public static void thenThrown(Class actualThrowableClazz)
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);
actualThrowableClazz
- the expected type of the caught throwable.Copyright © 2014. All rights reserved.