Verification of exceptions in junit

Generally whenever a test throws any exception, JUnit fails that test. But some times the exceptions are expected So test should pass when the exception occur. Below example will illustrate how to test exceptions in JUnit tests. Note how we have used expected parameter in @Test annotation.
@Test(expected = ArithmeticException.class)
public void testException(){
  System.out.println("Running Exception test");
  int k = 0 ;
  int b = 100/k;

Above test will pass after execution as the Arithmetic exception is thrown in last line. So that’s how we can check for any kind of exception in JUnit tests.

Web development and Automation testing

solutions delivered!!