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

Mockito ClassCastException - 無法投射模擬

Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
本文介紹了Mockito ClassCastException - 無法投射模擬的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想測試 AppleProcessor 類中有一個方法:

I have a method in the class AppleProcessor which I would like to test:

public void process(Fruit fruit) {
    if(fruit.getType() == Fruit.APPLE) {
        fruitBasket.add(((AppleFruit) fruit).getApple());
    }
    else {
        // do something else
    }
}

注意,Fruit 是 AppleFruit 實現的方法 getType() 的接口,并且還有一個 getApple() 方法.

Note that Fruit is an interface with the method getType() which AppleFruit implements and also has a getApple() method.

我的測試看起來像:

@Mock
FruitBasket fruitBasket;

@Mock
Fruit fruit;

@Mock
AppleFruit apple;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void testAnAppleIsProcessed() {
    AppleProcessor appleProcessor = new AppleProcessoer();
    when(fruit.getType()).thenReturn(Fruit.APPLE);
    when(((AppleFruit) fruit).getApple()).thenReturn(apple);

    appleProcessor.process(fruit);

    verify(fruitBasket).add(isA(Apple.class));
}

但是我收到以下錯誤:

java.lang.ClassCastException: package.fruit.Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54 無法轉換為 package.fruit.AppleFruit

來自測試中的這一行

when(((AppleFruit)fruit).getApple()).thenReturn(apple);

有人知道如何解決這個問題,以便我可以測試我的代碼嗎?

Would anyone know how to resolve this so I can test my code?

推薦答案

當你說

@Mock
Fruit fruit;

你告訴 Mockito:fruit 變量應該是 Fruit 的一個實例.Mockito會動態創建一個實現Fruit的類(這個類是Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54),并創建這個類的一個實例.這個類沒有理由成為 AppleFruit 的實例,因為您沒有告訴 Mockito 該對象必須是 AppleFruit 類型.

You tell Mockito: the fruit variable should be an instance of Fruit. Mockito will dynamically create a class which implements Fruit (this class is Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54), and create an instance of this class. There's no reason for this class to be an instance of AppleFruit, since you didn't tell Mockito that the object had to be of type AppleFruit.

將其聲明為AppleFruit,其類型為AppleFruit.

Declare it as AppleFruit, and it will be of type AppleFruit.

這篇關于Mockito ClassCastException - 無法投射模擬的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
Is it possible to create a mock object that implements multiple interfaces with EasyMock?(是否可以使用 EasyMock 創建一個實現多個接口的模擬對象?)
主站蜘蛛池模板: h视频在线免费 | 99re99| 亚洲视频一区在线观看 | 日本久久久久久 | a级毛片免费高清视频 | 欧美色a v | 免费亚洲婷婷 | 国产一级毛片精品完整视频版 | av特级毛片 | 国产免费一级一级 | 91精品国产色综合久久 | 国产精品自拍啪啪 | 一区二区三区四区国产 | 男人天堂国产 | av在线天堂 | 国产一区二区三区 | 精品国产一区二区三区久久影院 | 91精品观看 | 中文字幕一区二区三区在线视频 | 中文字幕不卡在线观看 | 国产精品久久久久久一区二区三区 | 欧美成人h版在线观看 | 精品福利在线视频 | 99资源 | 国产91久久精品一区二区 | 91亚洲精品在线观看 | 成人国产免费视频 | 欧美亚洲高清 | 欧美国产视频一区二区 | 91精品一区二区三区久久久久 | 高清一区二区视频 | 精品免费国产视频 | 免费高潮视频95在线观看网站 | 午夜a区 | 亚洲成年影院 | 免费一区二区三区在线视频 | 日本高清aⅴ毛片免费 | 日韩中文字幕久久 | 美女视频网站久久 | 精品国产乱码久久久 | 亚洲精品电影在线观看 |