【发布时间】:2020-10-08 09:03:50
【问题描述】:
我希望我的 junit 测试覆盖包括来自接口的默认方法。以下是接口中的方法
public default String getServerAddress() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
return null;
}
}
我的目标是涵盖此方法的异常部分。所以我可能需要模拟 InetAddress 以使其抛出异常。但我不确定如何? 我尝试模拟 inetAddress 并执行以下操作,但似乎不起作用:
when(inetAddress.getLocalHost().getHostAddress()).thenThrow(Exception.class)
【问题讨论】:
标签: java testing junit mocking mockito