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

mockito 回調和獲取參數值

mockito callbacks and getting argument values(mockito 回調和獲取參數值)
本文介紹了mockito 回調和獲取參數值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我沒有任何運氣讓 Mockito 捕獲函數參數值!我正在模擬搜索引擎索引,而不是構建索引,我只是使用哈希.

I'm not having any luck getting Mockito to capture function argument values! I am mocking a search engine index and instead of building an index, I'm just using a hash.

// Fake index for solr
Hashmap<Integer,Document> fakeIndex;

// Add a document 666 to the fakeIndex
SolrIndexReader reader = Mockito.mock(SolrIndexReader.class);

// Give the reader access to the fake index
Mockito.when(reader.document(666)).thenReturn(document(fakeIndex(666))

我不能使用任意參數,因為我正在測試查詢的結果(即它們返回哪些文檔).同樣,我不想為每個文檔指定一個特定的值并有一行!

I can't use arbitrary arguments because I'm testing the results of queries (ie which documents they return). Likewise, I don't want to specify a specific value for and have a line for each document!

Mockito.when(reader.document(0)).thenReturn(document(fakeIndex(0))
Mockito.when(reader.document(1)).thenReturn(document(fakeIndex(1))
....
Mockito.when(reader.document(n)).thenReturn(document(fakeIndex(n))

我查看了 使用 Mockito 頁面上的回調部分.不幸的是,它不是 Java,我無法將自己的解釋用于 Java.

I looked at the callbacks section on the Using Mockito page. Unfortunately, it isn't Java and I couldn't get my own interpretation of that to work in Java.

編輯(澄清):如何讓 Mockito 捕獲參數 X 并將其傳遞給我的函數?我想將 X 的確切值(或引用)傳遞給函數.

EDIT (for clarification): How do I get get Mockito to capture an argument X and pass it into my function? I want the exact value (or ref) of X passed to the function.

我不想枚舉所有情況,并且任意參數將不起作用,因為我正在測試不同查詢的不同結果.

I do not want to enumerate all cases, and arbitrary argument won't work because I'm testing for different results for different queries.

Mockito 頁面顯示

The Mockito page says

val mockedList = mock[List[String]]
mockedList.get(anyInt) answers { i => "The parameter is " + i.toString } 

那不是java,我不知道如何翻譯成java或將發生的任何事情傳遞給函數.

That's not java, and I don't know how to translate into java or pass whatever happened into a function.

推薦答案

我沒用過 Mockito,但是想學,就這樣吧.如果有人比我不知道答案,請先嘗試他們的答案!

I've never used Mockito, but want to learn, so here goes. If someone less clueless than me answers, try their answer first!

Mockito.when(reader.document(anyInt())).thenAnswer(new Answer() {
 public Object answer(InvocationOnMock invocation) {
     Object[] args = invocation.getArguments();
     Object mock = invocation.getMock();
     return document(fakeIndex((int)(Integer)args[0]));
     }
 });

這篇關于mockito 回調和獲取參數值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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精品一区二区三区 | 国产精品精品久久久 | 日韩一区欧美一区 | 日韩在线一区二区三区 | 欧美日韩国产综合在线 | 俺去俺来也www色官网cms | 一级片在线免费播放 | 精品国产乱码久久久 | 国产 亚洲 网红 主播 | 全免费a级毛片免费看视频免费下 | 区一区二在线观看 | 亚洲色视频 | 91久色 | 午夜精品久久久久久不卡欧美一级 | av片在线免费看 | 日本在线中文 | 午夜小视频免费观看 | 亚洲精品456 | jizjizjiz中国护士18 | 九九九久久国产免费 | 97精品一区二区 | 午夜国产一级 | 国产精品国产成人国产三级 | 亚洲www啪成人一区二区麻豆 | 天天操夜夜看 | 欧美男人天堂 | 国产成人精品高清久久 | 亚洲一区二区久久 | 成年人黄色一级毛片 | 日韩第一页 | 亚洲3级 | 精品国产高清一区二区三区 | 国产精品美女久久久久久不卡 | 亚洲精品99 | 99精品免费久久久久久日本 | 欧美啊v在线观看 | 国产精品久久在线观看 | 国产一区二区三区在线视频 | 欧州一区|