First, let me give you the scenario:
interface TaskDone{
void OnTaskDone(Object data)
}
public class DownloadImage extends AsyncTask{
private TaskDone callback;
public DownloadImage(TaskDone callback){this.callback=callback}
@Override protected Bitmap doInBackground(String ...params){
Bitmap image= null;
// contact to server and download image
return image;
}
@Override protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
callback.onTaskDone(result);
}
}
}
Now I want to do Unit test that the image is being downloaded. As it is mandatory that the DownloadImage class need to be called from UI thread and I don't have any Activity class in my source packages. How can I write my unit testing? I have done something like the following but it seems to me that runOnUIThread is not running.
class DownloadImageTest {
@Before public void setUp() throws Exception {
countDownLatch = new CountDownLatch(1);
}
public void test1_TaskDownload_should_download_a_lanscape_banner() throws Throwable {
Context context = Mockito.mock(Context.class);
Context contextSpy = PowerMockito.spy(context);
Intent intent = new Intent(contextSpy, Activity.class);
contextSpy.startActivity(intent);
activity.startActivity(intent);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
DownloadImage downloadImage = new DownloadImage(new TaskDone() {
@Override
public void onTaskDone(Object data) {
rawImageLandscape = (Bitmap) data;
countDownLatch.countDown();
}
});
downloadImage.execute(URL);
}
});
try {
countDownLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Any help would be greatly appreciated. Thanks for reading the question too.enter code here
Aucun commentaire:
Enregistrer un commentaire