【发布时间】:2019-05-28 07:47:51
【问题描述】:
由于我在 catch 块中的 return 语句,我的 JUnit 测试没有捕获异常。当我删除返回语句时,测试通过。如果发生异常,我希望我的单元测试能够使用返回语句。
我也尝试过 JUnit 5 的东西,但它不能解决问题。
我的方法:
public ArrayList<Action> parsePlaByPlayTable() {
ArrayList<Action> actions = new ArrayList<>();
Document document = null;
try {
document = Jsoup.connect(url).get();
} catch (Exception e) {
log.error(e.getMessage());
return new ArrayList<>();
}
// if the exception occurs and there is no return in the catch block,
// nullPointerException is thrown here
Element table = document.getElementById("pbp");
// more code. . .
}
我的测试:
@Test(expected = Exception.class)
public void testParsePlaByPlayTableInvalidUrl() {
PlayByPlayActionHtmlParser parser = new PlayByPlayActionHtmlParser("https://www.basketbal-reference.com/oxscores/pbp/201905160GS.html");
ArrayList<Action> actions = parser.parsePlaByPlayTable();
}
【问题讨论】:
-
您正在捕获
Exception并在catch块中返回new ArrayList<>()。这意味着,您不能断言任何Exception。