You can use Mockitos Answer .
doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); ByteArrayOutputStream baos = (ByteArrayOutputStream)args[0]; //fill baos with data return null; } }).when(client).retrieveFile(baos);
However, if you have the opportunity to reorganize the tested code, it is better to make the client return an OutputStream or some data that can be placed in this output stream. It will be a much better design.
source share