Java mockito 回调和获取参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6631764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:25:21  来源:igfitidea点击:

mockito callbacks and getting argument values

javaunit-testingmockingmockito

提问by nflacco

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.

我没有运气让 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))

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 Mockitopage. Unfortunately, it isn't Java and I couldn't get my own interpretation of that to work in Java.

我查看了Using Mockito页面上的回调部分。不幸的是,它不是 Java,我无法在 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.

编辑(澄清):如何让 Mockito 捕获参数 X 并将其传递到我的函数中?我想要传递给函数的 X 的确切值(或引用)。

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

Mockito 页面说

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.

那不是java,我不知道如何翻译成java或将发生的任何事情传递给函数。

采纳答案by Ed Staub

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

我从未使用过 Mockito,但想学习,所以就这样吧。如果有人比我更无知,请先尝试他们的答案!

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]));
     }
 });

回答by qualidafial

Check out ArgumentCaptors:

查看 ArgumentCaptors:

https://site.mockito.org/javadoc/current/org/mockito/ArgumentCaptor.html

https://site.mockito.org/javadoc/current/org/mockito/ArgumentCaptor.html

ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
Mockito.when(reader.document(argument.capture())).thenAnswer(
  new Answer() {
    Object answer(InvocationOnMock invocation) {
      return document(argument.getValue());
    }
  });

回答by Jean-Fran?ois Beauchef

With Java 8, this could be something like this:

使用 Java 8,这可能是这样的:

Mockito.when(reader.document(anyInt())).thenAnswer(
  (InvocationOnMock invocation) -> document(invocation.getArguments()[0]));

I am assuming that documentis a map.

我假设那document是一张地图。

回答by fl0w

You might want to use verify() in combination with the ArgumentCaptor to assure execution in the test and the ArgumentCaptor to evaluate the arguments:

您可能希望将 verify() 与 ArgumentCaptor 结合使用以确保在测试中执行,并使用 ArgumentCaptor 来评估参数:

ArgumentCaptor<Document> argument = ArgumentCaptor.forClass(Document.class);
verify(reader).document(argument.capture());
assertEquals(*expected value here*, argument.getValue());

The argument's value is obviously accessible via the argument.getValue() for further manipulation / checking or whatever you wish to do.

显然可以通过 argument.getValue() 访问参数的值,以进行进一步的操作/检查或您想做的任何事情。