Android 机器人分子异步任务回调

Android 机器人分子异步任务回调,android,unit-testing,android-asynctask,robolectric,powermockito,Android,Unit Testing,Android Asynctask,Robolectric,Powermockito,我想测试在AsyncTask中执行的方法。我已经嘲笑了里面的所有方法。我所拥有的- 获取所有必需参数并执行新AsyncTask()的静态方法。executeOnExecutor() 在onPostExecute()中,我使用结果调用回调 对于我正在使用的测试: testCompile 'junit:junit:4.12' testCompile "org.robolectric:robolectric:3.2.2" testCompile "org.powermock:powermock

我想测试在AsyncTask中执行的方法。我已经嘲笑了里面的所有方法。我所拥有的-

  • 获取所有必需参数并执行新AsyncTask()的静态方法。executeOnExecutor()
  • 在onPostExecute()中,我使用结果调用回调
  • 对于我正在使用的测试:

        testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.2.2"
    testCompile "org.powermock:powermock-module-junit4:1.6.4"
    testCompile "org.powermock:powermock-module-junit4-rule:1.6.4"
    testCompile "org.powermock:powermock-api-mockito:1.6.4"
    testCompile "org.powermock:powermock-classloading-xstream:1.6.4"
    
    但回调永远不会被调用。例如:

      @RunWith(RobolectricTestRunner.class)
        @Config(constants = BuildConfig.class, sdk = 24,
                application = StubApplicationClass.class)
        @PowerMockIgnore({"org.mockito.", "android.*"})
        @PrepareForTest(BaseHttpClient.class)
    
        @SuppressStaticInitializationFor("io.realm.internal.Util")
        public class GetDataTest {
    
            @Rule
            public PowerMockRule rule = new PowerMockRule();
            ....................................
    
            @Test
            public void getDataTest() throws Exception {
            System.out.println("started");
            BaseAsyncClient.doAsync(UserPublicDataTable, Actions,
                    list, new IHttpResponse<String>() {
                        @Override
                        public void httpResponse(@Nullable String response) {
                            assertNotNull(response); System.out.println("onPostExecute()");
                        }
                    });
            System.out.println("finished");
        }
    
    我收到一个例外:

    java.lang.NullPointerException
        at org.robolectric.Robolectric.getBackgroundThreadScheduler(Robolectric.java:193)
        at org.robolectric.Robolectric.flushBackgroundThreadScheduler(Robolectric.java:200)
    
    如何使用robolectric、powermock测试AsyncTask?

    更新

    正如我看到的,
    ShadowApplication.getInstance()==null
    ,这就是为什么我在调用“
    flushBackgroundThreadScheduler
    ”时收到NPE异常

    我创建了,但它仍然是空的

    要修复迁移到的应用程序中的NPE,请执行以下操作:

    testCompile "org.robolectric:robolectric:3.0"
    
    更新:

    当我在@Test中创建AsyncTask时,它可以工作

    new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                System.out.println("empty async task");
                return null;
            }
        }.execute();
        ShadowApplication.runBackgroundTasks();
        Robolectric.flushBackgroundThreadScheduler();
    

    因此,当使用“静态”方法创建任务时,问题出现了。

    我通过一些解决方法解决了这个问题:

  • 将版本降级为testCompile“org.roblectric:roblectric:3.0”
  • 调用async tas new AsyncTask.execute()
  • 调用ShadowApplication.executeBackgroundTasks()
  • 后台执行线程时睡眠。睡眠(1000)
  • 使用ShadowScheduler执行foregroud任务
  • 不幸的是,这需要等待后台线程执行。我也没有找到用main替换后台线程的方法

    最后,我从AsyncTask迁移到RxJava。 在这里,我使用默认方法将线程池执行器替换为相同的线程执行:

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
     RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() {
                @Override
                public Scheduler apply(Scheduler scheduler) throws Exception {
                    return ImmediateThinScheduler.INSTANCE;
                }
            });
    
    编译'io.reactivex.rxjava2:rxandroid:2.0.1'
    编译“io.reactivex.rxjava2:rxjava:2.0.1”
    setIoSchedulerHandler(新函数(){
    @凌驾
    公共调度程序应用(调度程序)引发异常{
    返回ImmediateThinScheduler.INSTANCE;
    }
    });
    
    Schedulers.io()
    是线程池执行器,我将其替换为同一个线程执行器
    ImmediateThinScheduler.INSTANCE

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
     RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() {
                @Override
                public Scheduler apply(Scheduler scheduler) throws Exception {
                    return ImmediateThinScheduler.INSTANCE;
                }
            });