title: Mocking getter / setter method of Interface with Mockito
body: <p>I am trying to test my implementation with jUnit and Mockito and I am running into problems. Here is a very simplified example which explains the issue</p>
  
  <p>Interface KeyValueInterface </p>
  
  <pre><code>public interface KeyValueInterface {
  
      public abstract String getKey();
  
      public abstract void setKey(String key);
  
      public abstract String getValue();
  
      public abstract void setValue(String value);
  
  }
  </code></pre>
  
  <p>Class KeyValueImpl </p>
  
  <pre><code>public class KeyValueImpl implements KeyValueInterface {
  
      private String key;
      private String value;
  
      @Override
      public String getKey() {
          return key;
      }
  
      @Override
      public void setKey(String key) {
          this.key = key;
      }
  
      @Override
      public String getValue() {
          return value;
      }
  
      @Override
      public void setValue(String value) {
          this.value = value;
      }
  
  }
  </code></pre>
  
  <p>Class with "business logic"</p>
  
  <pre><code>public class ValueFinder {
  
      public KeyValueInterface findValueForKey(KeyValueInterface keyValue){
          keyValue.setValue("foo");
          return keyValue;
      }
  
  }
  </code></pre>
  
  <p>jUnit Test class</p>
  
  <pre><code>import static org.junit.Assert.*;
  
  import org.junit.Test;
  import org.mockito.Mockito;
  
  public class ValueFinderTest {
  
      @Test
      public void testNotMocked() {
          KeyValueInterface keyValue = new KeyValueImpl();
          keyValue = (new ValueFinder()).findValueForKey(keyValue);
          assertEquals("foo", keyValue.getValue()); // works fine
      }
  
      @Test
      public void testMocked1() {
          KeyValueInterface keyValue = Mockito.mock(KeyValueInterface.class);
          keyValue = (new ValueFinder()).findValueForKey(keyValue);
          assertEquals("foo", keyValue.getValue()); // java.lang.AssertionError:
                                                      // expected:&lt;foo&gt; but
                                                      // was:&lt;null&gt;
  
      }
  
      @Test
      public void testMocked2() {
          KeyValueInterface keyValue = Mockito.mock(KeyValueInterface.class);
          keyValue = (new ValueFinder()).findValueForKey(keyValue);
          Mockito.when(keyValue.getValue()).thenCallRealMethod();
          Mockito.doCallRealMethod().when(keyValue).setValue(Mockito.any(String.class));
          assertEquals("foo", keyValue.getValue()); // org.mockito.exceptions.base.MockitoException:
                                                      // Cannot call real method
                                                      // on java interface.
                                                      // Interface does not have
                                                      // any implementation!
                                                      // Calling real methods is
                                                      // only possible when
                                                      // mocking concrete classes.
  
      }
  
  }
  </code></pre>
  
  <p>My probelm is, that I need to mock KeyValue for technical reasons which are beyond my control. Therefore I cannot just go with method testNotMocked(). Also for technical reasons beyond my control I have to mock the interface (and not the class).</p>
  
  <p>Is there any way to achieve this?</p>
  
  <p>Thanks a lot.</p>