Exception Testing

We can write "@Test" to expect and exception in the attribute.

Ex:

@Test(expected = ArithmeticException.class)

public void testDiv()

{

System.out.println( "Running Exception test: testDiv" ) ;

int x1 ;

x1 = 5 / 0 ;

}

What we are stating is that the method should throw an Exception of type "ArithmeticException" and if it does then it is a success case else it is a failure.

Create another test case in CalculatorTest that creates an array of integers and then write a loop that exceeds the size of the array giving an "ArrayIndexOutOfBoundsException" exception.

Declare this as the expected exception as an attribute in the @Test method.

Exception Exercise