How to do unit test for Exceptions?

后端 未结 5 819
时光取名叫无心
时光取名叫无心 2020-12-14 15:43

As you know, exception is thrown at the condition of abnormal scenarios. So how to analog these exceptions? I feel it is challenge. For such code snippets:

p         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 16:24

    You can tell junit that the correct behavior is to get an exception.

    In JUnit 4, it goes something like:

    @Test(expected = MyExceptionClass.class) 
    public void functionUnderTest() {
        …
    }
    

提交回复
热议问题