gpt4 book ai didi

java - 抛出异常后继续

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:17 25 4
gpt4 key购买 nike

我在单元测试中抛出异常,但在抛出之后,我仍然希望能够继续测试

doThrow(new Exception()).when(myMock).myMethod();
myMock.myMethod();
System.out.println("Here"); // this is never called
// Do verify and asserts

这有可能吗?

最佳答案

你可以捕获异常:

doThrow(new MyException()).when(myMock).myMethod();

try {
myMock.myMethod();
fail("MyException should have been thrown!");
} catch (MyException expected) {
// Do something
}

System.out.println("Here");
// Verify the mock, assert, etc.

关于java - 抛出异常后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com