Robolectric AsyncTask 回调
Robolectric AsyncTask callback
我想测试在 AsyncTask 中执行的方法。我已经嘲笑了里面的所有方法。我有什么 -
- 获取所有必需参数并执行 new 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");
}
onPostExecute - 从不打印。
当我打电话时:
Robolectric.flushBackgroundThreadScheduler();
我收到异常:
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 异常的原因。
我创建了custom runner to create custom application,但它仍然是空的。
要修复迁移到的应用程序中的 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();
使用'static'方法创建任务时出现问题
我设法解决了这个问题work-around:
- 降级到testCompile "org.robolectric:robolectric:3.0"
- 调用异步任务 new AsyncTask.execute()
- 调用ShadowApplication.executeBackgroundTasks();
- 在后台执行时休眠 Thread.sleep(1000);
- 使用ShadowScheduler执行前台任务
不幸的是,这需要等到后台线程执行。我也没有找到用 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;
}
});
Schedulers.io()
是线程池执行器,我换成同一个线程执行器ImmediateThinScheduler.INSTANCE
我想测试在 AsyncTask 中执行的方法。我已经嘲笑了里面的所有方法。我有什么 -
- 获取所有必需参数并执行 new 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");
}
onPostExecute - 从不打印。
当我打电话时:
Robolectric.flushBackgroundThreadScheduler();
我收到异常:
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 异常的原因。
我创建了custom runner to create custom application,但它仍然是空的。
要修复迁移到的应用程序中的 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();
使用'static'方法创建任务时出现问题
我设法解决了这个问题work-around:
- 降级到testCompile "org.robolectric:robolectric:3.0"
- 调用异步任务 new AsyncTask.execute()
- 调用ShadowApplication.executeBackgroundTasks();
- 在后台执行时休眠 Thread.sleep(1000);
- 使用ShadowScheduler执行前台任务
不幸的是,这需要等到后台线程执行。我也没有找到用 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;
}
});
Schedulers.io()
是线程池执行器,我换成同一个线程执行器ImmediateThinScheduler.INSTANCE