title: Mockito matcher to match a method with generics and a supplier
body: <p>I'm using Java 1.8.0_131, Mockito 2.8.47 and PowerMock 1.7.0. My question is not related to PowerMock, it is released to a Mockito.when(…) matcher.</p>
  
  <p>I need a solution to mock this method which is called by my class under test:</p>
  
  <pre><code>public static &lt;T extends Serializable&gt; PersistenceController&lt;T&gt; createController(
      final Class&lt;? extends Serializable&gt; clazz,
      final Supplier&lt;T&gt; constructor) { … }
  </code></pre>
  
  <p>The method is called from the class under test like this:</p>
  
  <pre><code>PersistenceController&lt;EventRepository&gt; eventController =
      PersistenceManager.createController(Event.class, EventRepository::new);
  </code></pre>
  
  <p>For the test, I first create my mock object which should be returned when the above method was called:</p>
  
  <pre><code>final PersistenceController&lt;EventRepository&gt; controllerMock =
      mock(PersistenceController.class);
  </code></pre>
  
  <p>That was easy. The problem is the matcher for the method arguments because the method uses generics in combination with a supplier as parameters. The following code compiles and returns null as expected:</p>
  
  <pre><code>when(PersistenceManager.createController(any(), any()))
      .thenReturn(null);
  </code></pre>
  
  <p>Of course, I do not want to return null. I want to return my mock object. That does not compile because of the generics. To comply with the types I have to write something like this:</p>
  
  <pre><code>when(PersistenceManager.createController(Event.class, EventRepository::new))
      .thenReturn(controllerMock);
  </code></pre>
  
  <p>This compiles but the parameters in my when are not matchers, so matching does not work and null is returned. I do not know how to write a matcher that will match my parameters and return my mock object. Do you have any idea?</p>
  
  <p>Thank you very much
  Marcus</p>