Thursday, May 15, 2014

How to handle runtime exception in TestNG or how to use 'expectedExceptions' with @Test

Note- This example shows how to test a runtime exception.
If the method withException() throws any runtime Exception then it will be passed.

ex-
1)

import org.testng.annotations.Test;

public class RunTimeExceptions {
    @Test(expectedExceptions = Exception.class)
    public void withException(){
        int a = 5/0;
    }
}

2) This test case will fail-

import org.testng.annotations.Test;

public class RunTimeExceptions {
    @Test()
    public void withException(){
        int a = 5/0;
    }
}

No comments:

Post a Comment