Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 测试在AsyncTask中包含侦听器回调的类_Android_Junit_Mockito_Android Testing_Mockito Kotlin - Fatal编程技术网

Android 测试在AsyncTask中包含侦听器回调的类

Android 测试在AsyncTask中包含侦听器回调的类,android,junit,mockito,android-testing,mockito-kotlin,Android,Junit,Mockito,Android Testing,Mockito Kotlin,我正在尝试测试在异步任务中触发的侦听器回调 侦听器类: interface LoaderListener { fun onByteSuccess(..., ..., ...) fun onByteFailure(..., ...) } 包含异步任务的类: class Loader { override fun handleStreamTask(){ InputStreamHandlingTask(..., ...).execute(byteArray)

我正在尝试测试在
异步任务
中触发的侦听器回调

侦听器类:

interface LoaderListener {
    fun onByteSuccess(..., ..., ...)

    fun onByteFailure(..., ...)
}
包含异步任务的类:

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(..., ...)
                   }
            }

        }
}
我现在遇到的错误是:

线程中出现异常。。。java.lang.RuntimeException:在android.os.AsyncTask中执行的方法未模拟。有关详细信息,请参阅。 在android.os.AsyncTask.execute(AsyncTask.java)


如果这是在本地机器上而不是在Android设备上运行的单元测试,则不能模拟依赖于Android框架的类,例如
AsyncTask
。取而代之的是,我们应该将其实现为一个插装测试,而不是在Android设备上运行的单元测试,或者使用可以在本地机器上模拟Android框架的框架


更多信息:下面的示例演示了如何在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);
}
/**
*@可丢弃
*/
public void testAsynTask()抛出了Throwable{
//创建一个信号,让我们知道任务何时完成。
最终倒计时锁存器信号=新倒计时锁存器(1);
/*只需创建一个asynctask的内嵌实现
*通常不会这样做,只是为了完整性。
*您只需在项目中使用要进行单元测试的任务。
*/
final AsyncTask myTask=新建AsyncTask(){
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//要在后台线程中运行的代码。
返回“来自后台线程的预期值。”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
/*这是关键,通常您会使用某种类型的侦听器
*通知您的活动异步调用已完成。
*
*在您的测试方法中,您将订阅该文件并发送信号
*从那里取而代之。
*/
信号。倒计时();
}
};
//在UI线程上执行异步任务!这是关键!
runTestOnUiThread(新的Runnable(){
@凌驾
公开募捐{
myTask.execute(“做某事”);
}
});
/*测试线程将在这里等待,直到UI线程释放它
*上面的倒计时()或30秒过后,它将超时。
*/
等待信号(30,时间单位秒);
//任务完成了,现在您可以断言一些事情了!
资产真实(“预期价值”,真实);
}

既然没有其他人来回答这个问题,也许是时候看看不同的框架了。。。看这是正确的。要在设备上运行测试,第一步是将测试从测试目录移动到androidTest目录。
/**
 * @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);
}