Testing for multiple exceptions with JUnit 4 annotations

后端 未结 7 2254
-上瘾入骨i
-上瘾入骨i 2020-12-30 22:06

Is it possible to test for multiple exceptions in a single JUnit unit test? I know for a single exception one can use, for example

    @Test(expected=Illega         


        
相关标签:
7条回答
  • 2020-12-30 22:43

    Use catch-exception:

    // test 
    public void testDo() {
    
       // obj.do(1) must throw either A or B
       catchException(obj).do(1);
       assert caughtException() instanceof A
           || caughtException() instanceof B;
    
       // obj.do(2) must throw A but not SubclassOfA
       catchException(obj).do(2);
       assert caughtException() instanceof A
           && !(caughtException() instanceof SubclassOfA);
    
    }
    
    0 讨论(0)
提交回复
热议问题