Let's see how to list excepted exceptions that a test method can throw. If the test method throw no exception or throw other than excepted exceptions then the test will be marked a failure.
In the below example, there are two test methods -
1. exceptionTest - This test method result will be pass because the actual and expected exceptions is same (i.e. ArithmeticException).
2. exceptionTest1 - This test method result will be fail because the actual and expected exceptions is different.
Example Of TestNG expectedExceptions
import org.testng.annotations.Test; public class ExceptedExceptionTest { @Test(expectedExceptions = ArithmeticException.class) public void exceptionTest(){ int i = 6 / 0; } @Test(expectedExceptions = ArithmeticException.class) public void exceptionTest1(){ int i = 65675.7676 / 2; } }
Output: