public class BDDCatchException extends Object
EXAMPLE:
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 IndexOutOfBoundsException
then(caughtException())
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessage("Index: 1, Size: 0")
.hasNoCause();
// then we expect an IndexOutOfBoundsException (alternatively)
thenThrown(IndexOutOfBoundsException.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 IndexOutOfBoundsException
assertThat(caughtException())
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessage("Index: 1, Size: 0")
.hasMessageStartingWith("Index: 1")
.hasMessageEndingWith("Size: 0")
.hasMessageContaining("Size")
.hasNoCause();
Constructor and Description |
---|
BDDCatchException() |
Modifier and Type | Method and Description |
---|---|
static void |
thenThrown(Class actualExceptionClazz)
Throws an assertion if no exception is thrown or if an exception of an
unexpected type is thrown.
|
static <T> T |
when(T obj) |
public BDDCatchException()
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
.CatchException.catchException(Object)
public static void thenThrown(Class actualExceptionClazz)
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 IndexOutOfBoundsException
thenThrown(IndexOutOfBoundsException.class);
actualExceptionClazz
- the expected type of the caught exception.Copyright © 2014. All rights reserved.