Mockito callbacks and getting argument values

I was not lucky to get a Mockito to capture the values ​​of function arguments! I make fun of a search engine index and instead of creating an index, I just use 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 cannot use arbitrary arguments because I check the results of the queries (i.e. which documents they return). Similarly, I do not want to specify a specific value and have a row 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 of the Using Mockito page. Unfortunately, this is not Java, and I could not get my own interpretation of this to work in Java.

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

I do not want to list all cases, and an arbitrary argument will not work, because I am testing different results for different queries.

The Mockito page says:

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

This is not java, and I do not know how to translate into java or pass everything that happened to a function.

+45
java mockito
Jul 08 2018-11-11T00:
source share
4 answers

I never used Mokito, but I want to study, so here. If someone is less ignorant than me, answer, first try your answer!

 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])); } }); 
+56
Jul 09 '11 at 0:40
source share

Check out ArgumentCaptors:

http://site.mockito.org/mockito/docs/1.10.19/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()); } }); 
+38
Jul 16 '11 at 19:23
source share

You might want to use verify () in conjunction with an ArgumentCaptor to ensure execution in the test and an ArgumentCaptor to evaluate the arguments:

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

The value of the argument is obviously accessible through argument.getValue () for further processing / checking or what you want to do.

+8
Oct 28 '16 at 11:50
source share

With Java 8, it could be something like this:

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

I assume document is a mapping.

+4
Oct. 15 '16 at 19:55
source share



All Articles