This line redundant because you call the method under test in the following line.
The first of the two lines uses BDD/FEST syntax, and the second line uses a Hamcrest matcher. You don't have to mix these coding styles.The following complete example shows the differences between the coding styles. Each test uses its own coding style. Choose the coding style you prefer. Personally, I prefer the first one, because it is very easy to understand.package com.googlecode.catchexception.help;
import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.CatchException.verifyException;
import static com.googlecode.catchexception.apis.CatchExceptionBdd.then;
import static com.googlecode.catchexception.apis.CatchExceptionBdd.when;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.hasMessage;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.hasNoCause;
import static org.hamcrest.CoreMatchers.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class AddressManagerTest {
private static class AppException extends RuntimeException {
public AppException() {
super("Phooey!");
}
};
private static class AddressManager {
public void throwException() {
throw new AppException();
}
}
private AddressManager manager = new AddressManager();
@Test
public void testThrowException_usingStandardAPI_variant1()
throws AppException {
catchException(manager).throwException();
assertTrue(caughtException() instanceof AppException);
assertEquals("Phooey!", caughtException().getMessage());
assertNull(caughtException().getCause());
}
@Test
public void testThrowException_usingStandardAPI_variant2()
throws AppException {
verifyException(manager, AppException.class).throwException();
assertEquals(caughtException().getMessage(), "Phooey!");
assertNull(caughtException().getCause());
}
@Test
public void testThrowException_usingHamcrest() throws AppException {
catchException(manager).throwException();
assertThat(caughtException(), //
allOf( //
is(AppException.class), //
hasMessage("Phooey!"), //
hasNoCause() //
) //
);
}
@Test
public void testThrowException_usingFEST_API() throws AppException {
when(manager).throwException();
then(caughtException()) //
.isInstanceOf(AppException.class) //
.hasMessage("Phooey!") //
.hasNoCause();
}
}