[英]Mockito doThrow not throwing Exception
I want to execute the catch block of the below code.
public String getResult() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
throw new Exception(e);
}
}
我編寫測試用例如下,
@Test
public void getResultTest() throws Exception{
doThrow(Exception.class).when(mockInetAddress).getLocalHost();
mockTestClassObj.getResult();
//code for verify exception
}
這執行得很好,而不是拋出異常。 在這種情況下我不能拋出異常嗎?
getLocalHost() 是一個 static 方法,所以你不能輕易模擬它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.