JUnit 4 Expected Exception type

前端 未结 4 1957
無奈伤痛
無奈伤痛 2020-12-08 06:54

I am trying to do a JUnit test on code that someone else has written, but I cannot figure out how to test for the exception, because the exception seems to lack a type.

4条回答
  •  臣服心动
    2020-12-08 07:18

    You can use JUnit 'expected' to test exceptions:

    @Test(expected = ExceptionYouWishToTestFor.class)  
    public void divisionWithException() {  
        // Test Code
    }
    

    After that it's up to you to throw that particular exception in your code.

提交回复
热议问题