pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

JMockit 中有沒有辦法從模擬方法調用原始方法?

Is there a way in JMockit to call the original method from a mocked method?(JMockit 中有沒有辦法從模擬方法調用原始方法?)
本文介紹了JMockit 中有沒有辦法從模擬方法調用原始方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的模擬類中,我正在模擬方法 foo().對于某些測試用例,我希望 foo() 的模擬實現返回一個特殊值.對于其他測試用例,我想使用 foo() 的真正實現.我在我的模擬類中定義了一個布爾值,以便我可以在模擬方法中確定是要返回特殊值還是使用真實"方法.問題是,我似乎無法弄清楚如何從模擬方法中調用真實方法.

In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or use the "real" method. The problem is, I can't seem to figure out how to call the real method from the mocked method.

我發現您可以在名為it"的模擬對象中定義一個特殊成員(具有被模擬對象的類型).這允許您從模擬實現中引用真實的類.所以,我的計劃是,如果我需要調用 foo() 的真實"實現,模擬方法將調用 it.foo().但是,這不起作用,因為調用 it.foo() 只是再次調用模擬版本,而不是真實版本,所以我最終得到了無限遞歸.

I found that you can define a special member within the mock object named "it" (with type of the object being mocked). This allows you to reference the real class from the mock implementation. So, my plan was, if I needed to invoke the "real" implementation of foo(), the mock method would call it.foo(). However, this doesn't work, because calling it.foo() just calls the mock version again, not the real version, so I end up with infinite recursion.

有什么辦法可以讓這個工作嗎?

Is there some way to make this work?

使用代碼示例可能會更清楚,這是我當前的模擬方法實現的樣子:

it might be clearer with a code example, here's what my current mocked method implementation looks like:

private RealClass it;
...
public SomeClass foo() {
    if(fakeIt) {
        return new SomeClass("fakevalue");
    } else {
        // doesn't work, just keeps calling the mock foo
        // in infinite recursion
        return it.foo();
    }
}

編輯 2:另外,對于我的大多數測試用例,我確實想要模擬實現.所以我最初的嘗試是只在我需要模擬對象的那些測試用例中調用 Mockit.redefineMethods() .但這不起作用 - 似乎您只能在 setup/teardown 中執行此操作......當我嘗試這樣做時,我的模擬實現從未被調用.

EDIT 2: Also, for most of my test cases I do NOT want the mock implementation. So my initial attempt at this was to only call Mockit.redefineMethods() within those test cases where I needed the mock object. But this didn't work - it seems you can only do this within setup/teardown ... my mock implementation never got called when I tried that.

解決方案說明:

起初我不認為給出的答案有效,但在玩了一些之后,問題似乎是我將 JMockit 核心"方法與注釋"驅動方法混合在一起.顯然,在使用注釋時,您需要使用 Mockit.setupMocks,而不是 Mockit.redefineMethods().這就是最終奏效的方法:

At first I didn't think the answer given worked, but after playing with it some more, it seems the problem is that I was mixing JMockit "core" methods with the "annotation" driven methods. Apparently when using the annotation you need to use Mockit.setupMocks, not Mockit.redefineMethods(). This is what finally worked:

@Before 
public void setUp() throws Exception
{
    Mockit.setUpMocks(MyMockClass.class);
}

然后,對于模擬類:

@MockClass(realClass = RealClass.class)
public static class MyMockClass {
    private static boolean fakeIt = false;
    private RealClass it;

    @Mock(reentrant = true)
    public SomeClass foo() {
        if(fakeIt) {
            return new SomeClass("fakevalue");
        } else {
            return it.foo();
        }
    }
}

推薦答案

我認為你可以使用 @Mock 注釋來做到這一點.從文檔中,您的模擬類上的 @Mock(reentrant=true) 應該這樣做.

I think you can do this with the @Mock annotation. From the docs, @Mock(reentrant=true) on your mock class should do it.

請參閱 http://jmockit.googlecode.com/svn/trunk/www/javadoc/mockit/Mock.html
例如看這里 http://jmockit.googlecode.com/svn/trunk/www/tutorial/StateBasedTesting.html#reentrant

不過我還沒有測試過..

I haven't tested this though..

這篇關于JMockit 中有沒有辦法從模擬方法調用原始方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 深圳律师咨询_深圳律师事务所_华荣【免费在线法律咨询】网 | 好笔杆子网 - 公文写作学习交流分享平台| 膜结构车棚|上海膜结构车棚|上海车棚厂家|上海膜结构公司 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | IWIS链条代理-ALPS耦合透镜-硅烷预处理剂-上海顶楚电子有限公司 lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 迪威娱乐|迪威娱乐客服|18183620002 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 西装定制/做厂家/公司_西装订做/制价格/费用-北京圣达信西装 | 萃取箱-萃取槽-PVC萃取箱厂家-混合澄清槽- 杭州南方化工设备 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | U拓留学雅思一站式服务中心_留学申请_雅思托福培训 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 消防设施操作员考试报名时间,报名入口,报考条件 | 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 飞歌臭氧发生器厂家_水处理臭氧发生器_十大臭氧消毒机品牌 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 艺术漆十大品牌_艺术涂料加盟代理_蒙太奇艺术涂料厂家品牌|艺术漆|微水泥|硅藻泥|乳胶漆 | IP检测-检测您的IP质量| 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 线材成型机,线材折弯机,线材成型机厂家,贝朗自动化设备有限公司1 | 横河变送器-横河压力变送器-EJA变送器-EJA压力变送器-「泉蕴仪表」 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 净化工程_无尘车间_无尘车间装修-广州科凌净化工程有限公司 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 影合社-影视人的内容合作平台| 厂房出租-厂房规划-食品技术-厂房设计-厂房装修-建筑施工-设备供应-设备求购-龙爪豆食品行业平台 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 |