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

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

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

問題描述

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

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

我讀過這個(gè)問題:問題

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

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

如果超類方法足夠大,則此答案另一個(gè)答案不適合.就我而言,我有非常大的代碼.是的,我知道它違反了 SOLID OOD 原則,但我應(yīng)該編寫測試.我沒有足夠的時(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.

該問題是 4 年前提出的!

said question was asked 4 years ago!

目前 Mockito 或 Powermock 是否可以解決此問題?

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

和測試:

@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ì)我有用,但替換不行.見下圖:

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)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
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精品久久久久久久久 | 日韩精品久久久久 | 91av视频在线观看 | 羞羞午夜 | 午夜成人在线视频 | 欧美成人一级 | 亚洲国产中文字幕 | 亚洲色图插插插 | 成人一区二区三区在线观看 | 国产激情精品 | 91精品国产色综合久久 | 国产成人精品一区二区三区在线 | 久久精品视频网站 | av天天澡天天爽天天av | 免费观看成人av | 国产精品第2页 | 日韩精品一区二区三区视频播放 | 日韩www视频 | 亚洲欧美日韩在线一区二区 | 亚洲人成在线观看 | 亚洲一区二区三区在线 | 亚洲成人中文字幕 | 看真人视频一级毛片 | 天天躁日日躁狠狠的躁天龙影院 | 久久久久久久久淑女av国产精品 | 在线视频国产一区 | 免费网站国产 | 91免费高清 | 久久久久久av | 国产欧美日韩在线一区 | 在线观看电影av | 黄色av网站在线免费观看 | 日韩欧美成人精品 | 精品国产乱码久久久久久闺蜜 | 一区中文字幕 | 国产片侵犯亲女视频播放 | 久久综合久久久 | 精品国产乱码久久久久久a丨 | av中文字幕在线播放 | 欧美一区二区在线看 | h片在线观看免费 |