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

Mockito ClassCastException - 無(wú)法投射模擬

Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無(wú)法投射模擬)
本文介紹了Mockito ClassCastException - 無(wú)法投射模擬的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想測(cè)試 AppleProcessor 類中有一個(gè)方法:

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
    }
}

注意,F(xiàn)ruit 是 AppleFruit 實(shí)現(xiàn)的方法 getType() 的接口,并且還有一個(gè) getApple() 方法.

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

我的測(cè)試看起來(lái)像:

@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));
}

但是我收到以下錯(cuò)誤:

java.lang.ClassCastException: package.fruit.Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54 無(wú)法轉(zhuǎn)換為 package.fruit.AppleFruit

來(lái)自測(cè)試中的這一行

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

有人知道如何解決這個(gè)問(wèn)題,以便我可以測(cè)試我的代碼嗎?

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

推薦答案

當(dāng)你說(shuō)

@Mock
Fruit fruit;

你告訴 Mockito:fruit 變量應(yīng)該是 Fruit 的一個(gè)實(shí)例.Mockito會(huì)動(dòng)態(tài)創(chuàng)建一個(gè)實(shí)現(xiàn)Fruit的類(這個(gè)類是Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54),并創(chuàng)建這個(gè)類的一個(gè)實(shí)例.這個(gè)類沒(méi)有理由成為 AppleFruit 的實(shí)例,因?yàn)槟鷽](méi)有告訴 Mockito 該對(duì)象必須是 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.

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

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫(kù)連接)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
Is it possible to create a mock object that implements multiple interfaces with EasyMock?(是否可以使用 EasyMock 創(chuàng)建一個(gè)實(shí)現(xiàn)多個(gè)接口的模擬對(duì)象?)
主站蜘蛛池模板: 成人国产在线观看 | 亚洲视频中文字幕 | 精品三区| 四虎影院免费在线 | 欧美成人一区二免费视频软件 | 国产97在线视频 | 久久一区二区三区四区 | 精品国产亚洲一区二区三区大结局 | 国产精品麻 | 狠狠热视频 | 日韩一区二区三区视频 | 欧美中文视频 | 日本在线看片 | 性大毛片视频 | 成人午夜免费在线视频 | 日韩中文字幕在线视频 | 99热在线观看精品 | 米奇7777狠狠狠狠视频 | 国产婷婷精品av在线 | 91精品国产乱码久久久久久久久 | 一区二区不卡视频 | 国产精品99久久久精品免费观看 | 久久久久久久国产 | 欧美在线观看免费观看视频 | 日韩视频一区二区在线 | 毛片一级片 | 国产精品精品 | 色av一区 | 久久亚洲国产精品日日av夜夜 | 美女露尿口视频 | 亚洲视频在线一区 | 午夜激情在线 | 亚洲一区视频 | 五月婷婷婷 | 成年人黄色小视频 | 亚洲视频一区二区三区 | 日韩久草 | 成人av一区二区三区 | 国产成人免费视频 | 97av视频在线 | 久久久久久艹 |