# Dealing with AsyncTask in Unit Tests

DevFeed: [Dealing with AsyncTask in Unit Tests](<https://devfeed.tech/articles/dealing-with-asynctask-in-unit-tests-30583.md>)

Original publisher: [Read original article](<https://ryanharter.com/blog/2015/12/dealing-with-asynctask-in-unit-tests/>)

Published: 2015-12-28T07:00:00Z

Content type: tutorial

Language: en

Sources: [Blogs on Ryan Harter](<https://devfeed.tech/sources/blogs-on-ryan-harter.md>)

Topics: [unit tests](<https://devfeed.tech/topics/unit-tests.md>), [Android](<https://devfeed.tech/topics/android.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [apk](<https://devfeed.tech/tags/apk.md>), [code](<https://devfeed.tech/tags/code.md>), [tests](<https://devfeed.tech/tags/tests.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

This tutorial explains how to make Android AsyncTask-based unit tests deterministic by overriding AsyncTask in the test source set with a synchronous implementation. It also describes scope and environment cautions for this technique.

## Source excerpt

There may be a shortage of love on the internet for AsyncTasks, but that doesn't mean they don't have their uses. I've found myself using them a fair bit in the latest project that I'm working on. All in all, they make offloading tasks from the main thread quite simple, but can pose some challenges, particularly in tested environments. The biggest challenge with using AsyncTask in tested code is that, since the code runs asynchronously, it can be difficult to ensure your tests get the right result for verification. I have seen some solutions for getting around this dilemma, but they involve pretty significant changes to the structure of your app, and exposing some internal members, simply for the sake of the tests.