title: Mocking generic interfaces with Kotlin and Mockito
body: <p>I am trying to mock a generic interface in Kotlin using Mockito. But so far I have found no natural solution. Given:</p>
  
  <pre><code>interface X&lt;T&gt; {
      fun x(): T
  }
  
  fun f(x: X&lt;Int&gt;) = x.x()
  </code></pre>
  
  <p>I could mock <code>X</code> with any of the following:</p>
  
  <ol>
  <li><p><code>val x = f(Mockito.mock(X::class.java) as  X&lt;Int&gt;)</code></p>
  
  <p>But that would generate an "unchecked cast" warning.</p></li>
  <li><p><code>@Mock lateinit var x: X&lt;Int&gt;</code></p>
  
  <p>But I do not want to use the <code>@Mock</code> annotation because I like to have my fields final.</p></li>
  <li><p>Introduce a helper function, as the <a href="https://github.com/nhaarman/mockito-kotlin" rel="noreferrer" title="mockito-kotlin library">mockito-kotlin library</a> does:</p>
  
  <p><code>inline fun &lt;reified T : Any&gt; mock(): T = Mockito.mock(T::class.java)!!</code></p>
  
  <p>Then call it like this:</p>
  
  <p><code>val x: X&lt;Int&gt; = mock()</code></p>
  
  <p>But I do not want to use helper functions.</p></li>
  </ol>
  
  <p>Is there an elegant pure Kotlin way to mock a generic interface with Mockito?
  (I would prefer a version of 1. without the warning.)</p>