This is sample repository to understand Exception Test in TestNG through different examples. Exception test basically helps to validate the execptionn raised in case an unexpected event occurs in execution.
TestNG is an open-source automation testing framework for JAVA built on the capabilities of JUnit and NUnit. It helps to write testcases in a well-structured, easy-to-maintain way by means of annotations, attributes, and other features.
An exception test is a test case that confirms a particular exception is raised whenever a certain code block is executed. Exception testing is an important part of software testing, as it helps to validate that the application behaves as expected when faced with exceptional scenarios. To write such a test, the TestNG testing framework is used.
Using the expectedExceptions attribute of the @Test annotation, we can write exception tests in TestNG. This attribute accepts a single exception class or a list of exception classes that the test function is expected to throw. The test will succeed if any of the predefined exceptions are raised. The test will fail if a different exception or no exception is thrown. Using the expectedExceptionsMessageRegExp attribute, we can also confirm the exception message.
@Test(expectedExceptions = NoSuchElementException.class)
public void test_single_exception()
{
//code which throws NoSuchElementException
}
@Test(expectedExceptions = {NoSuchElementException.class, NullPointerException.class})
public void test_multiple_exception()
{
//code which can throw either of
//NoSuchElementException or NullPointerException
}
@Test(expectedExceptions = NumberFormatException.class, expectedExceptionsMessageRegExp = "Cannot parse null string")
public void test_assert_exception_message()
{
//code to throw exception with similar exception message
}
It is created using Selenium with Java, TestNG and Maven for Web-based automation to handle SSL certificates.
This is the list of tools, being used in this framework:
- Apache Maven
- Java 8
- Selenium Cloud Grid - LambdaTest Platform
- TestNG Framework
- Import this project in Eclipse/IntelliJ as “Existing Maven Project”
- Go to the test file and Run test case for your desired case.
- You can see the logs coming up on Console as your execution progresses.
- Since we are using RemoteWebDriver and executing on Cloud Grid platform, LambdaTest, you can login to same and view detailed logs on dashboard.