title: Test a class that involves listener callback inside AsyncTask
body: <p>I'm trying to test a listener callback that is being triggered inside an <code>AsyncTask</code>,</p>
  
  <p>Listener class :</p>
  
  <pre><code>interface LoaderListener {
      fun onByteSuccess(..., ..., ...)
  
      fun onByteFailure(..., ...)
  }
  </code></pre>
  
  <p>The class containing AsyncTask :</p>
  
  <pre><code>class Loader {
  
  override fun handleStreamTask(){
          InputStreamHandlingTask(..., ...).execute(byteArray)
      }
  
  private inner class InputStreamHandlingTask constructor(
              internal var ...,
              internal var ...
          ) : AsyncTask&lt;ByteArray, Void, ByteArray&gt;() {
  
              override fun doInBackground(vararg params: ByteArray): ByteArray? {
                  val response = params[0]
                  .....
                  return response
              }
  
              override fun onPostExecute(byteArray: ByteArray?) {
                     if (byteArray != null) {
                         listener.onByteSuccess(..., ..., ...)
                     } else {
                         listener.onByteFailure(..., ...)
                     }
              }
  
          }
  }
  </code></pre>
  
  <p>The test I'm trying to make :</p>
  
  <pre><code>@Test
  fun testIfListenerCalled(){
      val loader: Loader = mock()
      val loaderListener: LoaderListener = mock()
  
      loader.handleStreamTask()
  
      verify(loaderListener).onByteSuccess(..., ..., ...)
  }
  </code></pre>
  
  <p>The error I'm curently encountering:</p>
  
  <blockquote>
    <p><strong>Exception in thread ... java.lang.RuntimeException: Method execute in android.os.AsyncTask not mocked. See <a href="http://g.co/androidstudio/not-mocked" rel="nofollow noreferrer">http://g.co/androidstudio/not-mocked</a> for details.
            at android.os.AsyncTask.execute(AsyncTask.java)</strong></p>
  </blockquote>