久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Mockito 從模擬中拋出已檢查的異常

throw checked Exceptions from mocks with Mockito(使用 Mockito 從模擬中拋出已檢查的異常)
本文介紹了使用 Mockito 從模擬中拋出已檢查的異常的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試圖讓我的一個模擬對象在調用特定方法時拋出一個檢查異常.我正在嘗試以下方法.

I'm trying to have one of my mocked objects throw a checked Exception when a particular method is called. I'm trying the following.

@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
    List<String> list = mock(List.class);
    when(list.get(0)).thenThrow(new SomeException());
    String test = list.get(0);
}

public class SomeException extends Exception {
}

但是,這會產生以下錯誤.

However, that produces the following error.

org.testng.TestException: 
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException

查看 Mockito 文檔,他們只使用RuntimeException,難道不能用Mockito從一個mock對象中拋出checked Exceptions嗎?

Looking at the Mockito documentation, they only use RuntimeException, is it not possible to throw checked Exceptions from a mock object with Mockito?

推薦答案

檢查 列表.
get(int index) 方法被聲明為僅拋出擴展 RuntimeExceptionIndexOutOfBoundException.
您正試圖告訴 Mockito 拋出一個異常 SomeException(),該異常 該特定方法調用無法拋出該異常.

Check the Java API for List.
The get(int index) method is declared to throw only the IndexOutOfBoundException which extends RuntimeException.
You are trying to tell Mockito to throw an exception SomeException() that is not valid to be thrown by that particular method call.

進一步澄清.
List 接口不提供從 get(int index) 方法拋出一個經過檢查的異常,這就是 Mockito 失敗的原因.當您創建 mocked List 時,Mockito 將使用 List 的定義.class 來創建它的 mock.

To clarify further.
The List interface does not provide for a checked Exception to be thrown from the get(int index) method and that is why Mockito is failing.
When you create the mocked List, Mockito will use the definition of List.class to creates its mock.

您使用 when(list.get(0)).thenThrow(new SomeException()) 指定的行為 與 List API 中的方法簽名不匹配,因為 get(int index) 方法不會拋出 SomeException() 所以 Mockito 失敗.

The behavior you are specifying with the when(list.get(0)).thenThrow(new SomeException()) doesn't match the method signature in List API, because get(int index) method does not throw SomeException() so Mockito fails.

如果你真的想這樣做,那么讓 Mockito 拋出一個 new RuntimeException() 甚至更好地拋出一個 new ArrayIndexOutOfBoundsException() 因為 API 指定這是要拋出的唯一有效異常.

If you really want to do this, then have Mockito throw a new RuntimeException() or even better throw a new ArrayIndexOutOfBoundsException() since the API specifies that that is the only valid Exception to be thrown.

這篇關于使用 Mockito 從模擬中拋出已檢查的異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 欧美一级毛片久久99精品蜜桃 | 国产91亚洲精品 | 久久视频免费观看 | 91在线看| 精品国产一区一区二区三亚瑟 | 毛片a级毛片免费播放100 | 夜夜爽99久久国产综合精品女不卡 | 久久一区二区三区四区 | 国产精品我不卡 | 伊人久久伊人 | 91福利影院 | 欧美一区二区三 | 另类亚洲视频 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | www.伊人.com | 欧美日韩国产传媒 | 2021天天干夜夜爽 | 黄色三级免费 | 一道本不卡| 欧美精品一区二区三区四区五区 | 国产精品色哟哟网站 | 中文字幕视频一区 | 亚洲精品国产成人 | 国产精品一区在线观看 | 欧美国产91 | 91观看| 女生羞羞网站 | 色www精品视频在线观看 | 成人一区二区三区在线 | 伊人久久精品一区二区三区 | 久久久久久久久久性 | 欧美日韩国产欧美 | 一区二区三区电影网 | 欧美 中文字幕 | 成人免费视频在线观看 | 伊人久久麻豆 | 日韩精品av一区二区三区 | 中文字幕亚洲视频 | 亚洲一区高清 | 久久久.com | 欧美一区二区三区四区视频 |