久久久久久久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综合网 | 国产探花在线观看视频 | 亚欧精品一区 | 国产精品日韩一区二区 | 成人小视频在线观看 | 91福利影院 | 精品久久香蕉国产线看观看亚洲 | 99精品网站 | 久久99精品视频 | 羞羞视频免费在线 | 三级视频国产 | 四虎影院欧美 | 一区二区免费高清视频 | 天天搞天天操 | 男人天堂网址 | 国产亚洲一区二区三区在线观看 | 天天操天天拍 | 国产精品成人在线播放 | 国产综合网站 | 亚洲精品黄色 | 午夜噜噜噜| 久久久精品一区 | 亚洲精品久久久一区二区三区 | 日韩精品福利 | 国产九九av | 91国内在线观看 | 久久人体 | 午夜影院在线 | 亚洲精品天堂 | 久久国产精品-久久精品 | 久久久黑人 | 国产在线播| 久久精品久久久久久 | 四虎av电影 |