title: Is there a way in JMockit to call the original method from a mocked method?
body: <p>In my mock class, I'm mocking method foo().  For some test cases, I want the mock implementation of foo() to return a special value.  For other test cases, I want to use the real implementation of foo().  I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or use the "real" method.  The problem is, I can't seem to figure out how to call the real method from the mocked method.</p>
  
  <p>I found that you can define a special member within the mock object named "it" (with type of the object being mocked).  This allows you to reference the real class from the mock implementation.  So, my plan was, if I needed to invoke the "real" implementation of foo(), the mock method would call it.foo().  However, this doesn't work, because calling it.foo() just calls the mock version again, not the real version, so I end up with infinite recursion.</p>
  
  <p>Is there some way to make this work?</p>
  
  <p>EDIT: it might be clearer with a code example, here's what my current mocked method implementation looks like:</p>
  
  <pre><code>private RealClass it;
  ...
  public SomeClass foo() {
      if(fakeIt) {
          return new SomeClass("fakevalue");
      } else {
          // doesn't work, just keeps calling the mock foo
          // in infinite recursion
          return it.foo();
      }
  }
  </code></pre>
  
  <p>EDIT 2: Also, for most of my test cases I do <em>NOT</em> want the mock implementation.  So my initial attempt at this was to only call Mockit.redefineMethods() within those test cases where I needed the mock object.  But this didn't work - it seems you can only do this within setup/teardown ... my mock implementation never got called when I tried that.</p>
  
  <p>NOTES ON SOLUTION:</p>
  
  <p>At first I didn't think the answer given worked, but after playing with it some more, it seems the problem is that I was mixing JMockit "core" methods with the "annotation" driven methods.  Apparently when using the annotation you need to use Mockit.setupMocks, not Mockit.redefineMethods().  This is what finally worked:</p>
  
  <pre><code>@Before 
  public void setUp() throws Exception
  {
      Mockit.setUpMocks(MyMockClass.class);
  }
  </code></pre>
  
  <p>Then, for the mock class:</p>
  
  <pre><code>@MockClass(realClass = RealClass.class)
  public static class MyMockClass {
      private static boolean fakeIt = false;
      private RealClass it;
  
      @Mock(reentrant = true)
      public SomeClass foo() {
          if(fakeIt) {
              return new SomeClass("fakevalue");
          } else {
              return it.foo();
          }
      }
  }
  </code></pre>