Протестируйте класс, который включает обратный вызов слушателя внутри AsyncTask - PullRequest
4 голосов
/ 30 июня 2019

Я пытаюсь проверить обратный вызов слушателя, который запускается внутри 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)

Ответы [ 2 ]

1 голос
/ 11 июля 2019

Если это модульный тест, который выполняется на вашем локальном компьютере, а не на устройстве Android, вы не можете смоделировать классы, которые зависят от платформы Android, например AsyncTask.Вместо этого следует реализовать его в качестве инструментального теста вместо модульного теста, который выполняется на устройстве Android, или использовать инфраструктуру, которая может издеваться над платформой Android на вашем локальном компьютере.

Дополнительная информация здесь: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests

0 голосов
/ 12 июля 2019

В следующем примере демонстрируется, как тестировать асинхронные задачи в JUnit.

/**
 * @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);
}
...