我正在尝试测试在内部触发的侦听器回调AsyncTask
,
监听类:
interface LoaderListener {
fun onByteSuccess(..., ..., ...)
fun onByteFailure(..., ...)
}
包含 AsyncTask 的类:
class Loader {
override fun handleStreamTask(){
InputStreamHandlingTask(..., ...).execute(byteArray)
}
private inner class InputStreamHandlingTask constructor(
internal var ...,
internal var ...
) : AsyncTask<ByteArray, Void, ByteArray>() {
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(..., ...)
}
}
}
}
我正在尝试进行的测试:
@Test
fun testIfListenerCalled(){
val loader: Loader = mock()
val loaderListener: LoaderListener = mock()
loader.handleStreamTask()
verify(loaderListener).onByteSuccess(..., ..., ...)
}
我目前遇到的错误:
线程中的异常... java.lang.RuntimeException:在 android.os.AsyncTask 中执行的方法未模拟。有关详细信息,请参阅http://g.co/androidstudio/not-mocked。在 android.os.AsyncTask.execute(AsyncTask.java)