Styrow.dev
Question 10 of 14
TestNG Topic: General medium

What is expected annotation?

Question

What is expected annotation?

Answer

We use ""expected"" with @Test annotation and specify the type of exceptions that are expected to be thrown when executing the test methods.

The below example which throws an exception called ""ArithmeticException"" when dividing two numbers with denominator value as 0. The test will pass since we handled the exception by ""expected"" annotation.

@Test(expected=ArithmeticException.class) public void dividedByZeroEx(){ int e=1/0; }

When you run the test without ""(expected = ArithmeticException.class)"", then it will throw the exception ""java.lang.ArithmeticException: / by zero"". And the test will fail.

@Test public void dividedByZeroExample2(){ int e=1/0; }