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

鏈式調用的模擬或存根

mock or stub for chained call(鏈式調用的模擬或存根)
本文介紹了鏈式調用的模擬或存根的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

protected int parseExpire(CacheContext ctx) throws AttributeDefineException {
    Method targetMethod = ctx.getTargetMethod();
    CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);
    ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class);
    // check for duplicate setting
    if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) {
        throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr");
    }
    // expire time defined in @CacheEnable or @ExpireExpr
    return cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE ? cacheEnable.expire() : parseExpireExpr(cacheExpire, ctx.getArgument());
}

這是測試的方法,

Method targetMethod = ctx.getTargetMethod();
CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class);

我必須模擬三個 CacheContext、Method 和 CacheEnable.有什么想法可以讓測試用例變得更簡單嗎?

I have to mock three CacheContext,Method and CacheEnable. Is there any idea to make the test case much simpler?

推薦答案

Mockito 可以處理鏈式存根:

Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);

// note that we're stubbing a chain of methods here: getBar().getName()
when(mock.getBar().getName()).thenReturn("deep");

// note that we're chaining method calls: getBar().getName()
assertEquals("deep", mock.getBar().getName());

AFAIK,鏈中的第一個方法返回一個模擬,它被設置為在第二個鏈接方法調用時返回您的值.

AFAIK, the first method in the chain returns a mock, which is set up to return your value on the second chained method call.

Mockito 的作者指出,這應該僅用于遺留代碼.更好的做法是將行為推送到您的 CacheContext 中,并提供它自己完成工作所需的任何信息.您從 CacheContext 提取的信息量表明您的課程具有 功能嫉妒.

Mockito's authors note that this should only be used for legacy code. A better thing to do otherwise is to push the behavior into your CacheContext and provide any information it needs to do the job itself. The amount of information you're pulling from CacheContext suggests that your class has feature envy.

這篇關于鏈式調用的模擬或存根的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 日本黄色一级视频 | 久久久久久国产一区二区三区 | 91成人小视频 | 国产精品一区久久久 | 一级毛片免费完整视频 | 亚洲精品久久久久久国产精华液 | 免费视频一区二区 | 久草视频观看 | 中文在线视频 | 视频一区二区三区中文字幕 | 偷拍自拍网 | 亚洲不卡av在线 | 欧产日产国产精品国产 | 成人无遮挡毛片免费看 | 成人免费毛片片v | 亚洲国产成人精品久久 | 北条麻妃视频在线观看 | 亚洲国产成人精品女人久久久野战 | 欧美欧美欧美 | 超碰电影| 91欧美激情一区二区三区成人 | 欧美国产视频 | 日韩午夜网站 | 在线播放中文字幕 | 欧洲国产精品视频 | 午夜在线电影网 | 欧美激情综合网 | 看黄在线 | av毛片| 久草成人 | 另类a v| 91av在线免费观看 | 国产精品毛片av一区 | 久久伊人青青草 | h视频在线免费 | 91在线精品视频 | 国产成人jvid在线播放 | 欧美一级大片 | 国产a爽一区二区久久久 | 成人在线精品视频 | 天天综合网91|