'When using kotlin coroutines, how do I unit test a function that calls a suspend function?

I have a class like this

class SomeClass {
    fun someFun() {
        // ... Some synchronous code
        async {
            suspendfun() 
        }
    }

    private suspend fun suspendFun() {
         dependency.otherFun().await()
         // ... other code
    }
}

I want to unit test someFun() so I wrote a unit test that looks like this:

@Test
fun testSomeFun() {
    runBlocking {
        someClass.someFun()
    }

    // ... verifies & asserts
}

But this doesn't seem to work because runBlocking doesn't actually block execution until everything inside runBlocking is done. If I test suspendFun() directly inside runBlocking it works as expected but I want to be able to test someFun() all together.

Any clue how to test a function with both sync and async code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source