title: How to mock generic method in Java with Mockito?
body: <p>How can we mock the <code>IRouteHandlerRegistry</code>? The error is <code>Cannot resolve method thenReturn(IHandleRoute&lt;TestRoute&gt;)</code></p>
  
  <pre><code>public interface RouteDefinition { }
  
  public class TestRoute implements RouteDefinition { }
  
  public interface IHandleRoute&lt;TRoute extends RouteDefinition&gt; {
      Route getHandlerFor(TRoute route);
  }
  
  public interface IRouteHandlerRegistry {
      &lt;TRoute extends RouteDefinition&gt; IHandleRoute&lt;TRoute&gt; getHandlerFor(TRoute route);
  }
  
  @Test
  @SuppressWarnings("unchecked")
  public void test() {
      // in my test
      RouteDefinition route = new TestRoute(); // TestRoute implements RouteDefinition
      IRouteHandlerRegistry registry = mock(IRouteHandlerRegistry.class);
      IHandleRoute&lt;TestRoute&gt; handler = mock(IHandleRoute.class);
  
      // Error: Cannot resolve method 'thenReturn(IHandleRoute&lt;TestRoute&gt;)'
      when(registry.getHandlerFor(route)).thenReturn(handler);
  }
  </code></pre>