Expect exception when using JUnit Theory
exception, java, junit, junit4, testing
Solution
I prefer using ExpectedException rule:
import org.junit.rules.ExpectedException;
<...>
@Rule
public ExpectedException thrown = ExpectedException.none();
@Theory
public void throwExceptionIfArgumentIsIllegal(Type type) throws Exception {
assumeThat(type, equalTo(ILLEGAL));
thrown.expect(IllegalArgumentException.class);
//perform actions
}
Problem
In JUnit 4 you can declare expected exception using `@Test(expected = SomeException.class)` annotation. However, when testing is done using Theories, `@Theory` annotation does not have expected property. What is the best way to declare expected exception when testing Theories?