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

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

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

問(wèn)題描述

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

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

如果超類(lèi)方法足夠大,則此答案另一個(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í)類(lèi)上)?的文章就介紹到這了,希望我們推薦的答案對(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ì)象?)
主站蜘蛛池模板: 午夜成人免费视频 | 狠狠爱综合 | 久久亚洲国产精品日日av夜夜 | 牛牛热在线视频 | 欧美成年黄网站色视频 | 色吧综合| 国产精品福利久久久 | 羞羞的视频在线观看 | 91一区二区 | 看片wwwwwwwwwww | 国产日韩久久久久69影院 | 精品欧美激情在线观看 | 精品视频久久久久久 | 国产免费一区二区三区网站免费 | 日韩午夜网站 | 色噜噜亚洲男人的天堂 | 日日夜夜天天 | 国产欧美性成人精品午夜 | 精品视频在线观看 | 国产精品永久免费视频 | 国产二区在线播放 | 久草.com | 国精日本亚洲欧州国产中文久久 | 在线一区二区三区 | www.日韩| 国产精品久久久久久一区二区三区 | 中文字幕乱码一区二区三区 | 91精品久久久久久久久久入口 | 国产精品成人一区二区三区夜夜夜 | 欧美在线一区二区三区 | 成人欧美一区二区三区在线播放 | 欧美性猛交 | 国产成人区 | 欧美一区二区在线观看 | 激情一区二区三区 | 久久激情视频 | 人人操日日干 | 国产欧美综合在线 | av影音在线 | 嫩草研究影院 | 自拍偷拍亚洲视频 |