Unit testing coroutines on UI thread


I’m using coroutines to do an asynchronous call on pull to refresh like so:

class DataFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener {

    // other functions here

    override fun onRefresh() {

    private fun loadDataAsync() = async(UI) {
        swipeRefreshLayout?.isRefreshing = true

        val data = async(CommonPool) {
            service?.getData() // suspending function

        when {
            data == null -> showError()
            data.isEmpty() -> progressLayout?.showEmpty(null, parentActivity?.getString(R.string.no_data), null)
            else -> {

        swipeRefreshLayout?.isRefreshing = false

Everything here works fine when I actually put it on a device. My error, empty, and data states are all handled well and the performance is good. However, I’m also trying to unit test it with Spek. My Spek test looks like this:

class DataFragmentTest : Spek({

    describe("The DataFragment") {

        var uut: DataFragment? = null
        beforeEachTest {
            uut = DataFragment()

        // test other functions
        describe("when onRefresh") {
            beforeEachTest {
                uut?.swipeRefreshLayout = mock()

            it("sets swipeRefreshLayout.isRefreshing to true") {
                verify(uut?.swipeRefreshLayout)?.isRefreshing = true // says no interaction with mock

The test is failing because it says that there was no interaction with the uut?.swipeRefreshLayout mock. After some experimenting, it seems this is because I’m using the UI context via async(UI). If I make it just be a regular async, I can get the test to pass but then the app crashes because I’m modifying views outside of the UI thread.

Any ideas why this might be occurring? Also, if anyone has any better suggestions for doing this which will make it more testable, I’m all ears.


EDIT: Forgot to mention that I also tried wrapping the verify and the uut?.onRefresh() in a runBlocking, but I still had no success.


You can’t use android stuf like a Handler and Looper on JVM.
You must run it like instrumentation test or u can apply some di logic to your fragment and for test use different context