Android 机器人分子异步任务回调
我想测试在AsyncTask中执行的方法。我已经嘲笑了里面的所有方法。我所拥有的-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
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();
因此,当使用“静态”方法创建任务时,问题出现了。我通过一些解决方法解决了这个问题:
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;
}
});