Open In App

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 TestNG

Testing 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.

Examples

1: 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");
        }
    }
}

Output

E-O
Output

2: 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");
        }
    }
}

Output

M-O
Output

3: 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");
        }
    }
}

Output

E-S
Output

Conclusion

Testing 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.


Next Article

Similar Reads