mockito 回调并获取参数值
我没有运气让 Mockito 捕获函数参数值!我正在嘲笑搜索引擎索引,而不是构建索引,我只是使用哈希。
// 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))
我不能使用任意参数,因为我正在测试查询的结果(即它们返回哪些文档)。同样,我不想为每个文档指定特定值并为每个文档指定一行!
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 中工作。
编辑(为了澄清): 如何让 Mockito 捕获参数 X 并将其传递到我的函数中?我想要将 X 的确切值(或引用)传递给函数。
我不想枚举所有情况,并且任意参数都不起作用,因为我正在测试不同查询的不同结果。
Mockito 页面说
val mockedList = mock[List[String]]
mockedList.get(anyInt) answers { i => "The parameter is " + i.toString }
That's not java,我不知道如何翻译成 java 或将发生的任何事情传递到函数中。
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))
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.
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.
The Mockito page says
val mockedList = mock[List[String]]
mockedList.get(anyInt) answers { i => "The parameter is " + i.toString }
That's not java, and I don't know how to translate into java or pass whatever happened into a function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我从未使用过 Mockito,但想学习,所以就开始吧。如果有人比我更无知地回答,请先尝试他们的答案!
I've never used Mockito, but want to learn, so here goes. If someone less clueless than me answers, try their answer first!
查看 ArgumentCaptor:
https://site.mockito.org/javadoc/current /org/mockito/ArgumentCaptor.html
Check out ArgumentCaptors:
https://site.mockito.org/javadoc/current/org/mockito/ArgumentCaptor.html
您可能希望将 verify() 与 ArgumentCaptor 结合使用,以确保测试中的执行,并使用 ArgumentCaptor 来评估参数:
参数的值显然可以通过 argument.getValue() 访问,以进行进一步的操作/检查或任何您想要的操作做。
You might want to use verify() in combination with the ArgumentCaptor to assure execution in the test and the ArgumentCaptor to evaluate the arguments:
The argument's value is obviously accessible via the argument.getValue() for further manipulation / checking or whatever you wish to do.
对于 Java 8,这可能是这样的:
我假设
document
是一个地图。With Java 8, this could be something like this:
I am assuming that
document
is a map.