title: How to mock generic method using mockito?
body: <p>I am trying to mock a generic method but getting class cast exception, no matter what I try. Class under testing is </p>
  
  <pre><code>public class TClass {
      private byte[] data;
      public &lt;T extends SInterface&gt; T asObject(T item){
          item.SMethod(this.getData());
          return item;
      }
  
      private byte[] getData() {
          return data;
      }
  }
  </code></pre>
  
  <p>while the <strong>testing class</strong> is </p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class TClassTest {
      @Mock
      SSInterface sClass;
      @Mock
      SObject sObject;
  
      TClass tClass = new TClass();
  
  
      @org.junit.Before
      public void setUp() throws Exception {
      }
  
      @org.junit.Test
      public void asObject() {
          Mockito.when(tClass.asObject(new SSClass())).thenReturn(sClass);
      }
  }
  </code></pre>
  
  <p>Other <strong>helper classes</strong> and <strong>interface</strong> are:</p>
  
  <pre><code> public interface SInterface {
      public void SMethod(byte[] in);
  }
  
  
  
  public interface SSInterface extends SInterface {
  }
  
  
  public class SSClass implements SSInterface {
      @Override
      public void SMethod(byte[] in) {
      }
  }
  
  public class SObject {
  }
  </code></pre>
  
  <p>The error I am getting is :</p>
  
  <blockquote>
    <p>Cannot resolve method thenReturn(generic.SSInterface)</p>
  </blockquote>
  
  <p>if I use <code>Mockito.when(tClass.asObject(new SSClass())).thenReturn(new SSClass());</code></p>
  
  <p>I get 
  org.mockito.exceptions.misusing.MissingMethodInvocationException: 
  when() requires an argument which has to be 'a method call on a mock'.
  For example:
      when(mock.getArticles()).thenReturn(articles);</p>
  
  <p>Also, this error might show up because:
  1. you stub either of: final/private/equals()/hashCode() methods.
     Those methods <em>cannot</em> be stubbed/verified.
     Mocking methods declared on non-public parent classes is not supported.
  2. inside when() you don't call method on mock but on some other object.</p>
  
  <pre><code>at generic.TClassTest.asObject(TClassTest.java:26)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78)
  at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)
  at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
  at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>