Expected Exception Test

It's used to test the exception throw by the method.

import org.junit.*;

/**
 * JUnit Expected Exception Test
 * @author Mithilesh
 *
 */
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.