是否可以在单个JUnit单元测试中测试多个异常?例如,我知道可以使用一个例外
@Test(expected=IllegalStateException.class)
Run Code Online (Sandbox Code Playgroud)
现在,如果我想测试另一个异常(例如,NullPointerException),可以在相同的注释,不同的注释中完成吗,还是我需要完全编写另一个单元测试?
我需要编写一个JUnit测试用例,它将测试一个传递不同排列的函数,并得到相应的结果.
成功的用例不返回任何内容,而失败的排列会抛出异常(异常类型无关紧要).
例如.testAppleisSweetAndRed(水果,颜色,味道)
测试会调用以下内容 -
testAppleisSweetAndRed(orange,red,sweet)//throws exception
testAppleisSweetAndRed(apple,green,sweet)//throws exception
testAppleisSweetAndRed(apple,red,sour)//throws exception
testAppleisSweetAndRed(apple,red,sweet)//OK
Run Code Online (Sandbox Code Playgroud)
如果调用的行为符合预期,则测试成功.
断言如何捕获前3次调用以确保它们确实引发预期的异常?