Monday 14 July 2014

Expected Exception Test

It’s used to test the exception throw by the method.
import org.junit.*;
 
/**
 * JUnit Expected Exception Test
 * 
 *
 */
public class JunitTest2 {
 
 @Test(expected = ArithmeticException.class)  
 public void divisionWithException() {  
   int i = 1/0;
 }  
 
}
In above example, the divisionWithException() method will throw an ArithmeticException Exception, since this is an expected exception, so the unit test will pass.

No comments:

Post a Comment