This section contains the detail about Testing for exceptions
Testing for exceptions in JUnit
The Test annotation supports 'expected' parameters which declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails. For example, the following test succeeds :
package devmanuals.junit;
import java.util.ArrayList;
import org.junit.Test;
public class ExceptionTest {
@Test(expected = IndexOutOfBoundsException.class)
public void outOfBounds() {
new ArrayList<Object>().get(1);
}
}
Output
Since it throws the expected exception that's why test is successful :


[ 0 ] Comments