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

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

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

問題描述

有時當我編寫單元測試時,我應該模擬對超類的引用.

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

我讀過這個問題:問題

此答案answer with DI 建議重構代碼.但我做不到

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

如果超類方法足夠大,則此答案另一個答案不適合.就我而言,我有非常大的代碼.是的,我知道它違反了 SOLID OOD 原則,但我應該編寫測試.我沒有足夠的時間進行重構.

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() 完成的操作.您還可以使方法對抑制不做任何事情.您甚至可以抑制靜態初始化程序塊.

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.

請閱讀 Powermock 作者的這篇博文.請參閱更換"一章.

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

更新:

抑制似乎對我有用,但替換不行.見下圖:

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

這篇關于如何模擬超級參考(在超級類上)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
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 創建一個實現多個接口的模擬對象?)
主站蜘蛛池模板: 国产午夜精品一区二区三区嫩草 | 欧美美女被c| 亚洲一级黄色 | av在线免费不卡 | 午夜影院 | 在线免费观看毛片 | 成人精品一区二区 | 日韩影院在线 | 国产午夜视频 | 欧区一欧区二欧区三免费 | 手机在线不卡av | 天天色天天射天天干 | 精品一区国产 | 亚洲三级国产 | 国产日韩精品在线 | 国产免费视频 | 成年精品 | 成人免费精品 | 欧美日韩一 | 成人黄色av网站 | a级片在线观看 | 337p日韩| 欧美成人h版在线观看 | 欧美视频1 | 免费观看的av毛片的网站 | 精品欧美一区二区久久久伦 | 日韩精品一区二区三区在线 | 久久久天天 | 99精品视频在线观看免费播放 | 成人午夜影院 | 亚洲一区二区久久久 | 操到爽| 一区二区视屏 | 日韩欧美国产一区二区三区 | 久久精品国产一区老色匹 | 在线观看成人 | 九色国产| 国产一级电影在线 | 日日夜夜精品视频 | www.久久久久久久久久久 | 中文字幕日韩一区 |