title: Using Mockito, How do I spy Kotlin class with val interface field declaration?
body: <p>I was trying to test retrofit's api client class in Kotlin using Mockito in Android, which looks like this:</p>
  
  <pre><code>class SomeApiClient : SomeApi {
  
     private val service: SomeApiService
  
     constructor(service: SomeApiService) {
       this.service = service
     }
  }
  </code></pre>
  
  <p>it implements <code>SomeApi</code> interface and contains one retrofit service interface as constructor parameter.</p>
  
  <p>When <code>spy</code>ing classes with Mockito, the classes need to have no-arg constructors otherwise your tests will fail with <code>MockitoException</code>:</p>
  
  <blockquote>
    <p>org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'SomeClass'.
    Please ensure that the type 'SomeClass' has a no-arg constructor.</p>
  </blockquote>
  
  <p>If classes have constructors with all parameters containing default values, then Kotlin generates parameterless constructors for it. However, what if the class receives <code>interface</code> as constructor parameter (and has corresponding field with <code>val</code> declaration), then how can we apply default value to it or declare empty constructor?</p>
  
  <p>Thanks!</p>