title: Mockito - intercept any method invocation on a mock
body: <p>Is it possible to intercept all method invocations on a mock in a generic way?</p>
  
  <p><strong>Example</strong></p>
  
  <p>Given a vendor provided class such as:</p>
  
  <pre><code>public class VendorObject {
  
      public int someIntMethod() {
          // ...
      }
  
      public String someStringMethod() {
          // ...
      }
  
  }
  </code></pre>
  
  <p>I would like to create a mock that re-directs <em>all</em> method calls to another class where there are matching method signatures:</p>
  
  <pre><code>public class RedirectedToObject {
  
      public int someIntMethod() {
          // Accepts re-direct
      }
  
  }
  </code></pre>
  
  <p>The when().thenAnswer() construct in Mockito seems to fit the bill but I cannot find a way to match any method call with any args.  The InvocationOnMock certainly gives me all these details anyway.  Is there a generic way to do this? Something that would look like this, where the when(vo.*) is replaced with appropriate code:</p>
  
  <pre><code>VendorObject vo = mock(VendorObject.class);
  when(vo.anyMethod(anyArgs)).thenAnswer(
      new Answer() {
          @Override
          public Object answer(InvocationOnMock invocation) {
  
              // 1. Check if method exists on RedirectToObject.
              // 2a. If it does, call the method with the args and return the result.
              // 2b. If it does not, throw an exception to fail the unit test.
  
          }
      }
  );
  </code></pre>
  
  <p>Adding wrappers around the vendor classes to make mocking easy is not an option because:</p>
  
  <ol>
  <li>Too large an existing code base.</li>
  <li>Part of extremely performance critical applications.</li>
  </ol>