测试一个涉及AsyncTask内的侦听器回调的类

时间:2019-06-30 07:36:47

标签: android junit mockito android-testing mockito-kotlin

我正在尝试测试在AsyncTask内部触发的监听器回调,

Listener类:

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)上

2 个答案:

答案 0 :(得分:1)

如果这是在本地计算机上而不是在Android设备上运行的单元测试,则不能模拟依赖于Android框架的类,例如AsyncTask。取而代之的是,应将其实现为有工具的测试,而不是在Android设备上运行的单元测试或使用可在本地计算机上模拟Android框架的框架。

此处有更多信息:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests

答案 1 :(得分:0)

下面的示例演示了如何在JUnit中测试Asynctasks。

/**
 * @throws Throwable
 */
public void testAsynTask () throws Throwable {
    // create  a signal to let us know when our task is done.
    final CountDownLatch signal = new CountDownLatch(1);

    /* Just create an in line implementation of an asynctask. Note this
     * would normally not be done, and is just here for completeness.
     * You would just use the task you want to unit test in your project.
     */
    final AsyncTask<String, Void, String> myTask = new AsyncTask<String, Void, String>() {

        @Override
        protected String doInBackground(String... arg0) {
            //Your code to run in background thread.
            return "Expected value from background thread.";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            /* This is the key, normally you would use some type of listener
             * to notify your activity that the async call was finished.
             *
             * In your test method you would subscribe to that and signal
             * from there instead.
             */
            signal.countDown();
        }
    };

    // Execute the async task on the UI thread! THIS IS KEY!
    runTestOnUiThread(new Runnable() {

        @Override
        public void run() {
            myTask.execute("Do something");
        }
    });

    /* The testing thread will wait here until the UI thread releases it
     * above with the countDown() or 30 seconds passes and it times out.
     */
    signal.await(30, TimeUnit.SECONDS);

    // The task is done, and now you can assert some things!
    assertTrue("Expected Value", true);
}