I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework.
It works just fine when I instantiate and e
This can be used if you want to test the result from the doInBackground
method. Override the onPostExecute
method and perform the tests there. To wait for the AsyncTask to complete use CountDownLatch. The latch.await()
waits till the countdown runs from 1 (which is set during initialization) to 0 (which is done by the countdown()
method).
@RunWith(AndroidJUnit4.class)
public class EndpointsAsyncTaskTest {
Context context;
@Test
public void testVerifyJoke() throws InterruptedException {
assertTrue(true);
final CountDownLatch latch = new CountDownLatch(1);
context = InstrumentationRegistry.getContext();
EndpointsAsyncTask testTask = new EndpointsAsyncTask() {
@Override
protected void onPostExecute(String result) {
assertNotNull(result);
if (result != null){
assertTrue(result.length() > 0);
latch.countDown();
}
}
};
testTask.execute(context);
latch.await();
}