This is what I want to achieve.
MyClass doSomething = mock(MyClass.class);
when(doSomething.perform(firstParameter, any(Integer.class), any(File.class)))
.thenReturn(firstParameter);
Basically I want the mocked class's method to always return the first argument that was passed into the method.
I have tried to use ArgumentCaptor, like so
ArgumentCaptor<File> inFileCaptor = ArgumentCaptor.forClass(File.class);
MyClass doSomething = mock(MyClass.class);
when(doSomething.perform(inFileCaptor.capture(), any(Integer.class), any(File.class)))
.thenReturn(firstParameter);
But mockito just failed with this error message:
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()
Examples of correct argument capturing:
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());
I think the ArgumentCaptor class only works with verify calls.
So how can I return the first parameter as passed-in during test?