Skip to content

Instantly share code, notes, and snippets.

@mariuszs
Last active January 4, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariuszs/8543918 to your computer and use it in GitHub Desktop.
Save mariuszs/8543918 to your computer and use it in GitHub Desktop.
BDD Style Catch Exception
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.then;
import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.when;
public class FooBarTest {
FooBar sut = new FooBar(); // System Under Test
@Test
public void shouldThrowExceptionWhenListHasTooManyElements() {
when(sut).getMethod(listWithSize(101));
then(caughtException()).isInstanceOf(IllegalArgumentException.class);
}
private ArrayList<String> listWithSize(int size) {
return new ArrayList<String>(Arrays.asList(new String[size]));
}
public class FooBar {
public int getMethod(List<String> code) throws IllegalArgumentException {
if (code.size() > 100)
throw new IllegalArgumentException();
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment