久久久久久久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久久影院| 久色一区| 国产黄色大片 | 亚洲精品乱码久久久久久按摩 | 午夜网站视频 | 午夜免费电影院 | 中文在线一区二区 | 日韩在线一区二区三区 | 美国av片在线观看 | 国产成人免费视频 | 久久99国产精品 | 色婷婷在线视频 | 国产精品免费一区二区三区 | 综合久久综合久久 | 永久免费视频 | 精品国产乱码久久久久久图片 | 懂色中文一区二区三区在线视频 | 国产小视频在线 | 成人精品国产一区二区4080 | www日韩 | 国产精品久久性 | 国产免费xxx | 久久精品国产久精国产 | 91精品国产91久久久久久丝袜 | 日韩毛片中文字幕 | 69精品久久久久久 | 精品欧美一区二区三区久久久 | 久久人体视频 | 亚洲精品视频在线观看视频 | 亚洲日韩中文字幕一区 | 国户精品久久久久久久久久久不卡 | 日韩一区在线视频 | 中国一级大毛片 | 在线观看视频中文字幕 | 国产免费又色又爽又黄在线观看 | 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | 亚洲精品9999 | 色999视频| 一区二区三区亚洲 | 亚洲成人自拍网 | 99精品欧美一区二区蜜桃免费 |