Handling known exceptions during TestNG method execution
During automation, we may have the need the method to raise a known exception. i.e. it the said method throws known exception, we can mark it PASS. Without this feature, any exception thrown during TestNG method execution, will mark the test as failed.
Following code snippet demonstrates how to achieve this in TestNG
import java.io.IOException;
import java.security.acl.AclNotFoundException;
import org.testng.annotations.Test;
public class MultipleGroups {
@Test(expectedExceptions = { AclNotFoundException.class })
public void exceptionTestOne() throws Exception {
throw new AclNotFoundException();
}
@Test(expectedExceptions = { IOException.class, NullPointerException.class })
public void exceptionTestTwo() throws Exception {
throw new Exception();
}
}
Output:
——-
PASSED: exceptionTestOne
FAILED: exceptionTestTwo
org.testng.TestException:
Expected exception of any of types [class java.io.IOException, class java.lang.NullPointerException] but got java.lang.Exception
similarly if some other exception is raised (not as expected, the method will fail as usual).
import java.io.IOException;
import java.security.acl.AclNotFoundException;
import org.testng.annotations.Test;
public class MultipleGroups {
@Test(expectedExceptions = { IOException.class })
public void exceptionTestOne() throws Exception {
throw new AclNotFoundException();
}
@Test(expectedExceptions = { IOException.class, NullPointerException.class })
public void exceptionTestTwo() throws Exception {
throw new Exception();
}
}
Output:
——-
FAILED: exceptionTestOne << == TestOne failed, as expectedException was not thrown during method execution org.testng.TestException: Expected exception of type class java.io.IOException but got java.security.acl.AclNotFoundException --------- Other throwable exceptions in Java: AclNotFoundException ActivationException AlreadyBoundException ApplicationException AWTException BackingStoreException BadAttributeValueExpException BadBinaryOpValueExpException BadLocationException BadStringOperationException BrokenBarrierException CertificateException CloneNotSupportedException DataFormatException DatatypeConfigurationException DestroyFailedException ExecutionException ExpandVetoException FontFormatException GeneralSecurityException GSSException IllegalClassFormatException InterruptedException IntrospectionException InvalidApplicationException InvalidMidiDataException InvalidPreferencesFormatException InvalidTargetObjectTypeException IOException JAXBException JMException KeySelectorException LastOwnerException LineUnavailableException MarshalException MidiUnavailableException MimeTypeParseException MimeTypeParseException NamingException NoninvertibleTransformException NotBoundException NotOwnerException ParseException ParserConfigurationException PrinterException PrintException PrivilegedActionException PropertyVetoException ReflectiveOperationException RefreshFailedException RemarshalException RuntimeException SAXException ScriptException ServerNotActiveException SOAPException SQLException TimeoutException TooManyListenersException TransformerException TransformException UnmodifiableClassException UnsupportedAudioFileException UnsupportedCallbackException UnsupportedFlavorException UnsupportedLookAndFeelException URIReferenceException URISyntaxException UserException XAException XMLParseException XMLSignatureException XMLStreamException XPathException
0 comments