AsyncTaskで実行されるメソッドをテストします。私はすでに内部のすべてのメソッドを嘲笑した。私が持っているもの -Robolectric AsyncTaskコールバック
- すべての必要なパラメータを取得し、新しいAsyncTask()を実行する静的メソッド。executeOnExecutor();
- onPostExecute()で、私はresultを使ってコールバックを呼び出します。
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();
私は例外受け取る:私はrobolectric、powermockでAsyncTaskをテストするにはどうすればよい
java.lang.NullPointerException
at org.robolectric.Robolectric.getBackgroundThreadScheduler(Robolectric.java:193)
at org.robolectric.Robolectric.flushBackgroundThreadScheduler(Robolectric.java:200)
を?
更新:ときのコール 'flushBackgroundThreadScheduler
' 私はNPE例外を受け取る理由
私はShadowApplication.getInstance() == null
を見ると、それはです。
custom runner to create custom applicationを作成しましたが、それでもnullです。
は、アプリケーションでのNPEを修正するには、移行へ:testCompile "org.robolectric:robolectric:3.0"
UPDATE:
私は@TestでAsyncTaskを作成する - それが動作
new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
System.out.println("empty async task");
return null;
}
}.execute();
ShadowApplication.runBackgroundTasks();
Robolectric.flushBackgroundThreadScheduler();
が問題タスクを使用して作成されたときに " static 'method