Issue
The test is passing even when the AsyncTask fails to fetch the joke (Example- If the backend is offline).
In my AsyncTask's doInBackground method, I have-
catch (IOException e) {
return e.getMessage();
}
So, when an error occurs and an exception is caught, the AsyncTask will return a string representing the error message. Hence, the test will pass even when the AsyncTask failed to fetch the joke as the string is not null.
The source code of the project:
https://github.com/AhmedHamdan54/BuildItBigger2
Edit 1: One friend told me that I can solve making the catch statement return a specific String and the test make sure it doesn't return that String to pass. But I don't know how to make it.
Solution
From your GitHub:
@Test
public void testDoInBackground() throws Exception {
MainActivityFragment fragment = new MainActivityFragment();
fragment.testFlag = true;
new EndpointAsyncTask().execute(fragment);
Thread.sleep(5000);
assertTrue("Error: Fetched Joke = " + fragment.loadedJoke, fragment.loadedJoke != null);
}
That's not the right approach to testing the AsyncTasks
. The execution of the doInBackground()
method takes place in a background thread. It is not guaranteed to finish in 5 seconds.
Use the CountDownLatch
instead of suspending the thread. Check this answer for details: https://stackoverflow.com/a/3802487/2470550
The second thing is your AsyncTask
. Take a look at this part:
try {
return myApiService.tellJoke().execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
and
@Override
protected void onPostExecute(String result) {
....
mainActivityFragment.loadedJoke = result;
}
Note that the AsyncTask
returns a result even when the IOException
is thrown. I guess that returning null
instead of e.getMessage()
in the catch
block will fix the issue.
Answered By - Wojciech Januszek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.