由于我在catch块中的return语句,因此我的JUnit测试没有捕获异常.当我删除返回语句时,测试通过.如果发生异常,我希望我的单元测试与返回语句一起工作.
我也尝试过JUnit 5的东西,但不能解决问题.
我的方法:
public ArrayList parsePlaByPlayTable() {
ArrayList 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 actions = parser.parsePlaByPlayTable();
}