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

如何模擬超級(jí)參考(在超級(jí)類上)?

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
本文介紹了如何模擬超級(jí)參考(在超級(jí)類上)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

有時(shí)當(dāng)我編寫(xiě)單元測(cè)試時(shí),我應(yīng)該模擬對(duì)超類的引用.

Sometimes when I write unit tests I should to mock reference to superclass.

我讀過(guò)這個(gè)問(wèn)題:問(wèn)題

此答案answer with DI 建議重構(gòu)代碼.但我做不到

This answer answer with DI advice to refactor code. But I cannot it

如果超類方法足夠大,則此答案另一個(gè)答案不適合.就我而言,我有非常大的代碼.是的,我知道它違反了 SOLID OOD 原則,但我應(yīng)該編寫(xiě)測(cè)試.我沒(méi)有足夠的時(shí)間進(jìn)行重構(gòu).

this answer another answer is not suitable if superclass method is enough big. In my case I have very big code. Yes I know that it is brokes SOLID OOD principes but I just should to write test. I have not enough time for refactor.

該問(wèn)題是 4 年前提出的!

said question was asked 4 years ago!

目前 Mockito 或 Powermock 是否可以解決此問(wèn)題?

Does currently Mockito or Powermock can resolve this issue ?

代碼示例:

class BaseService {  
    public void save() {
      // a lot of code here! I cannot change this code.
    }  
}

public Childservice extends BaseService {  
    public void save(){  
        //logic for testing
        super.save();
       //logic for testing
    }  
} 

更新 2

public class Parent {
    public int save() {
         return 99;
    }   
}

public class Child extends Parent {
    public int save() {
        int i = super.save();
        return i*2;
    }
}

和測(cè)試:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Parent.class)
public class ParentTest {
    @Test
    public void testSave() {       
        PowerMockito.suppress(PowerMockito.methodsDeclaredIn(Parent.class));        
        System.out.println(new Child().save());
    }
}

輸出:198

推薦答案

使用 Powermock 您可以替換或抑制方法,因此可以更改 BaseService.save() 完成的操作.您還可以使方法對(duì)抑制不做任何事情.您甚至可以抑制靜態(tài)初始化程序塊.

With Powermock you can replace or suppress methods, so it is possible to change the action done by BaseService.save(). You can also make methods to do nothing with suppressing. You can even suppress static initializer blocks.

請(qǐng)閱讀 Powermock 作者的這篇博文.請(qǐng)參閱更換"一章.

Please read this blog entry of the Powermock authors. See chapter "Replacing".

更新:

抑制似乎對(duì)我有用,但替換不行.見(jiàn)下圖:

Suppress seems to work for me, but replace not. See the picture below:

這篇關(guān)于如何模擬超級(jí)參考(在超級(jí)類上)?的文章就介紹到這了,希望我們推薦的答案對(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)文檔推薦

Java mock database connection(Java 模擬數(shù)據(jù)庫(kù)連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無(wú)法投射模擬)
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ì)象?)
主站蜘蛛池模板: 国产成人三级一区二区在线观看一 | 91看片看淫黄大片 | 欧美激情中文字幕 | 三级在线观看视频 | 免费毛片基地 | 午夜精品视频在线 | 欧美日韩综合网 | 福利网站在线观看 | 成人夜色 | 日本免费黄色 | 国产欧美日韩在线观看 | 国产伦精品一区二区三区视频网站 | 怡红院久久 | 99亚洲精品| 一级黄色录相 | 天天干一干| 亚洲免费看片 | 福利视频网站 | 久久久一本 | 黄色片一级片 | 久久99精品国产麻豆91樱花 | 欧美日韩视频 | 毛片视频免费 | 男女啪啪无遮挡 | 精品少妇一区二区三区免费观 | 国产在线欧美 | 天堂成人在线 | 中日韩毛片 | 日韩av资源 | 激情福利视频 | 国产精品久久久久久99 | 国产精品亚洲精品 | 麻豆一区二区三区 | 国产成人精品av | 一区二区三区高清 | 欧美激情视频网站 | 91看片在线| 精品小视频 | 亚洲成人黄色 | 久久久久久91 | 欧美在线视频免费 |