title: Mock the rest of an abstract class but call the real methods in it?
body: <p>There is some interface called say called <code>Foo</code>.</p>
  
  <pre><code>interface Foo {
      void add(Object key, Object value);
      Object get(Object key);
      void someOtherMethodUnessentialToTesting();
  }
  </code></pre>
  
  <p>There is an implementation in the test called <code>MockFoo</code> that implements most methods in a "default" way (doing nothing, returning <code>null</code>, basically only implementing them so it compiles). However, it implements a couple to give real functionality, and they are methods that insert and read from a <code>Map</code>. (If the last bit wasn't essential I would just use a Mockito mock and wouldn't even be asking.)</p>
  
  <pre><code>// The current "bad mock"
  class MockFoo implements Foo {
  
      Map&lt;Object, Object&gt; map = new ...
  
      @Override
      void add(Object key, Object value) {
          map.put(key, value);
      }
  
      @Override
      Object get(Object key) {
          map.get(key);
      }
  
      @Override
      void someOtherMethodUnessentialToTesting()
      {}   
  }
  </code></pre>
  
  <p><strong>The problem is that because this is not a Mockito mock, every time the interface changes the test has to be updated.</strong> Yes, people should check better to fix all implementations of the interface they change, but there <em>really</em> shouldn't be an implementation in the test in the first place in my opinion.</p>
  
  <p>I am confused on how to solve this. My instinct is to make it abstract and implement only those methods then mock it somehow so that it calls those "real" methods when it needs them. I read that Mockito has a <code>thenDoRealMethod()</code> for stubs but this is only for returning values so it would not work on a void method.</p>
  
  <pre><code>// My abstract class I was trying to stub somehow
  abstract class FooForTest implements Foo {
  
      Map&lt;Object, Object&gt; map = new ...
  
      @Override
      void add(Object key, Object value) {
          map.put(key, value);
      }
  
      @Override
      Object get(Object key) {
          map.get(key);
      } 
  }
  </code></pre>
  
  <hr>
  
  <p>I realize this may be a design issue and adding in an <code>AbstractFoo</code> in the real code is probably best (because the add and get won't change really) but I am more curious if there is a way to fix this once-and-for-all just by modifying the test code.</p>