4

我正在尝试测试在内部触发的侦听器回调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)

4

2 回答 2

1

如果这是在本地机器上运行而不是在 Android 设备上运行的单元测试,则不能模拟依赖于 Android 框架的类,例如AsyncTask. 相反,应该将其实现为一种仪器测试,而不是在 Android 设备上运行的单元测试,或者使用可以在本地机器上模拟 Android 框架的框架。

更多信息在这里:https ://developer.android.com/training/testing/unit-testing/instrumented-unit-tests

于 2019-07-11T16:45:59.713 回答
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);
}
于 2019-07-12T09:01:59.413 回答