title: How would I mock a method of a real object and access the method arguments?
body: <p>I'm using <code>Mockito</code> to write some tests in my Java code. In my test, I have a real object, but I want to mock what happens in one of the methods and be able to access the method arguments.</p>
  
  <p>If I do this:</p>
  
  <pre><code>MyObj spyObj = spy(new MyObj());
  doReturn(false).when(spyObj).myMethod(anyObject(), anyObject(), anyList(), anyLong());
  </code></pre>
  
  <p>I can't access the arguments and do any sort of logic with them.</p>
  
  <p>However, if I use <code>Mockito.when()</code> like so:</p>
  
  <pre><code>when(spyObj.myMethod(anyObject(), anyObject(), anyList(), anyLong())).thenAnswer(invocation -&gt; {
    if (invocation.getArguments()[0] == 1) {
        // do stuff
        return true;
    }
    return false;
  });
  </code></pre>
  
  <p>the real method runs instead of the mocked method. How would I be able to have my test run my mocked method and be able to access the arguments inside the mocked method? Thanks!</p>