Ich möchte Methode testen, die in AsyncTask ausgeführt werden. Ich habe schon alle Methoden in mir verspottet. Was ich habe -Robolectric AsyncTask Rückruf
- Statische Methode, die alle erforderlichen Parameter erhalten und neue AsyncTask() ausführen. ExecuteOnExecutor();
- In OnPostExecute() ich Callback mit Ergebnis aufrufen.
Für Test Ich verwende:
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"
Aber Rückruf wird nie aufgerufen. Beispiel:
@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 - wird nie gedruckt.
Als ich nennen:
Robolectric.flushBackgroundThreadScheduler();
ich eine Ausnahme an:
java.lang.NullPointerException
at org.robolectric.Robolectric.getBackgroundThreadScheduler(Robolectric.java:193)
at org.robolectric.Robolectric.flushBackgroundThreadScheduler(Robolectric.java:200)
Wie kann ich AsyncTask mit robolectric, PowerMock testen?
aktualisieren:
Wie ich ShadowApplication.getInstance() == null
sehen, weshalb ich NPE Ausnahme, wenn Anrufe 'flushBackgroundThreadScheduler
' erhalten.
Ich erstellte custom runner to create custom application, aber es ist immer noch null.
zu beheben NPE in Anwendung migriert:
testCompile "org.robolectric:robolectric:3.0"
UPDATE:
Wenn ich AsyncTask in @Test schaffen - es funktioniert
new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
System.out.println("empty async task");
return null;
}
}.execute();
ShadowApplication.runBackgroundTasks();
Robolectric.flushBackgroundThreadScheduler();
So Problem, wenn Aufgabe mit 'erstellt wird, Static 'Methode