TestNG: How to test for mandatory exceptions?

后端 未结 7 1732
刺人心
刺人心 2020-12-06 09:21

I\'d like to write a TestNG test to make sure an exception is thrown under a specific condition, and fail the test if the exception is not thrown. Is there an easy way to do

相关标签:
7条回答
  • 2020-12-06 10:17

    Use @Test annotation to check expected exceptions.

    @Test(
        expectedExceptions = AnyClassThatExtendsException.class,
        expectedExceptionsMessageRegExp = "Exception message regexp"
    )
    

    Or if you don't want to check for exception message, only below is enough

    @Test(expectedExceptions = AnyClassThatExtendsException.class)
    

    In that way, you don't need to use ugly try catch block, just invoke you exception-thrower method inside the test.

    0 讨论(0)
提交回复
热议问题