How to test for mandatory exceptions in TestNG? Last Updated : 03 Oct, 2024 Comments Improve Suggest changes Like Article Like Report TestNG (Test Next Generation) is inspired by JUnit and NUnit but comes with new functionalities that make it much stronger and easier. This feature is one of the most important ones, this is the good ability to test for expected exceptions in your test methods. That is, if in your code you are ensuring certain conditions of errors, it leads to the behavior of the code that is robust and reliable software.Exceptions in TestNGTesting for exceptions in TestNG is a test to ensure whether the conditions of your application are being met when error conditions occur, or when your application accepts error input. The exception tested would ensure that your code either throws exceptions or handles them under certain conditions. TestNG has a few methods through which you can test for exceptions, and you can set up how your test method will throw certain exceptions during its execution time.Examples1: Testing for a Single Exception Java package testsuite; import org.testng.annotations.Test; public class ExceptionTest { @Test(expectedExceptions = IllegalArgumentException.class) public void testInvalidInput() { validateInput(-1); // This should throw IllegalArgumentException } public void validateInput(int number) { if (number < 0) { throw new IllegalArgumentException("Input must be non-negative"); } } } OutputOutput2: Testing for Multiple Exceptions Java package testsuite; import org.testng.annotations.Test; public class MultipleExceptionTest { @Test(expectedExceptions = {IllegalArgumentException.class, NullPointerException.class}) public void testMultipleExceptions() { validateInput(null); // This should throw NullPointerException validateInput(-5); // This should throw IllegalArgumentException } public void validateInput(Integer number) { if (number == null) { throw new NullPointerException("Input cannot be null"); } if (number < 0) { throw new IllegalArgumentException("Input must be non-negative"); } } } OutputOutput3: Testing with a Try-Catch Block Java package testsuite; import org.testng.Assert; import org.testng.annotations.Test; public class ExceptionMessageTest { @Test public void testExceptionMessage() { try { validateInput(-10); Assert.fail("Expected IllegalArgumentException was not thrown."); } catch (IllegalArgumentException ex) { Assert.assertEquals(ex.getMessage(), "Input must be non-negative"); } } public void validateInput(int number) { if (number < 0) { throw new IllegalArgumentException("Input must be non-negative"); } } } OutputOutputConclusionTesting for mandatory exceptions in TestNG is quite straightforward and might be achieved by the expectedExceptions attribute, together with the expectedExceptionsMessageRegExp in case you want an even more specific check. Alternatively, you can apply the try-catch block approach for an even more detailed check. The sooner your code learns to properly handle exceptions, the stronger your software is going to be. Comment More infoAdvertise with us Next Article How to test for mandatory exceptions in TestNG? N nikhithamcb76 Follow Improve Article Tags : Software Testing Testing Tools TestNG Similar Reads How to Control Test Execution Order in TestNG? Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also m 3 min read How to Create Test Suite in TestNG? In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Te 11 min read How to Execute Failed Test Cases in TestNG TestNG is a testing framework that simplifies many testing needs. It stands for Test Next Generation, an open-source test automation framework inspired by JUnit and NUnit. Think of TestNG as an updated version of the other two concepts. It provides additional features not previously available, such 7 min read How to Stop Suite Execution after First Failure in TestNG? TestNG is the popular Java testing framework that offers annotations, configurations, and features in regards to test execution management. While working with TestNG there are a few cases when you might need to stop the execution of your test suite on the first failed test not to run some additional 3 min read How to stop TestNG from running after a test fail? In TestNG, a popular testing framework for Java, managing test execution flow effectively is crucial for ensuring the reliability of your test suites. One common requirement is to stop TestNG from running subsequent tests after a failure. This can be particularly useful when a test failure might inv 3 min read Like