I want to test method that are executed in AsyncTask. I already mocked all methods inside. What I have -
- Static method that get all required parameters and do new AsyncTask().executeOnExecutor();
- In onPostExecute() I call callback with result.
For test I'm using:
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"
But callback is never called. Example :
@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 - is never printed.
When I call:
Robolectric.flushBackgroundThreadScheduler();
I receive an exception:
java.lang.NullPointerException
at org.robolectric.Robolectric.getBackgroundThreadScheduler(Robolectric.java:193)
at org.robolectric.Robolectric.flushBackgroundThreadScheduler(Robolectric.java:200)
How can I test AsyncTask with robolectric, powermock ?
Update:
As I see ShadowApplication.getInstance() == null
, that is why I receive NPE exception when calls 'flushBackgroundThreadScheduler
'.
I created custom runner to create custom application, but it is still null.
To fix NPE in Application migrated to:
testCompile "org.robolectric:robolectric:3.0"
UPDATE:
When I create AsyncTask in @Test - it works
new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
System.out.println("empty async task");
return null;
}
}.execute();
ShadowApplication.runBackgroundTasks();
Robolectric.flushBackgroundThreadScheduler();
So problem when task is created with 'static' method