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

JMockit 中有沒有辦法從模擬方法調(diào)用原始方法?

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

問題描述

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

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.

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

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?

使用代碼示例可能會更清楚,這是我當(dāng)前的模擬方法實(shí)現(xiàn)的樣子:

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:另外,對于我的大多數(shù)測試用例,我確實(shí)想要模擬實(shí)現(xiàn).所以我最初的嘗試是只在我需要模擬對象的那些測試用例中調(diào)用 Mockit.redefineMethods() .但這不起作用 - 似乎您只能在 setup/teardown 中執(zhí)行此操作......當(dāng)我嘗試這樣做時,我的模擬實(shí)現(xiàn)從未被調(diào)用.

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.

解決方案說明:

起初我不認(rèn)為給出的答案有效,但在玩了一些之后,問題似乎是我將 JMockit 核心"方法與注釋"驅(qū)動方法混合在一起.顯然,在使用注釋時,您需要使用 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();
        }
    }
}

推薦答案

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

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..

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

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
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è)置為模擬對象但獲取 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)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 亚洲啪啪一区 | 午夜精品一区二区三区在线视频 | 日韩毛片网 | 成人精品高清 | 欧美aⅴ在线观看 | 成人久久久 | 国产精品久久久久一区二区三区 | 午夜资源| 超黄视频网站 | 久久性 | 亚洲男人天堂网 | 久草免费在线视频 | 成人在线激情 | 91精品国产综合久久久久久丝袜 | 欧美精品一区二区在线观看 | 久久亚洲欧美日韩精品专区 | 性高湖久久久久久久久aaaaa | 国产综合精品一区二区三区 | 69av片| 欧美日韩福利视频 | 中文字幕在线播放不卡 | 国产精品久久久久久久久图文区 | 日韩中文字幕av | 日日夜夜91 | 久久综合亚洲 | 亚洲精品区 | 亚洲精品视频在线播放 | 一区二区三区亚洲精品国 | 亚洲最大福利网 | 日韩高清一区 | 黄色电影在线免费观看 | 欧美精品一区二区三区在线播放 | 国产精品九九 | 在线成人 | 亚洲在线高清 | 中文字幕观看 | 日韩一区欧美一区 | 一级黄色片免费在线观看 | 欧美精品一区三区 | 中文字幕在线视频精品 | 久久99精品国产自在现线小黄鸭 |