最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to unit test the suspend function to meet branch coverage using mockk - Stack Overflow

programmeradmin2浏览0评论

I wrote a Kotlin code and I want to write unit tests for it. But I found that the unit test code I wrote still cannot cover 100% branch coverage. What should I do it?

kotlin code:

class MyRepository @Inject constructor(
    @DefaultDispatcher private val dispatcher: CoroutineDispatcher,
    @ApplicationScope private val scope: CoroutineScope,
    val checkService:ICheckService = CheckService()
) {
    fun fetchData(value: Int): String {
        return runBlocking(dispatcher) {
            suspendCoroutine<String> { continuation ->
                println(111)
                scope.launch(dispatcher) {
                    println(222)
                    delay(100)
                    println(333)
                    if(checkService.check(value)){
                        println(444)
                    }
                    continuation.resume("result")
                }
            }
        }
    }
}

interface ICheckService {
    suspend fun check(value:Int): Boolean
}

class CheckService:ICheckService{
    override suspend fun check(value: Int): Boolean {
        // mock delay
        delay(300)

        return value > 0
    }

}

UT code:

class MyRepositoryTest4 {
    private lateinit var myRepository: MyRepository
    private val dispatcher = StandardTestDispatcher()
    private lateinit var scope: TestScope

    @Before
    fun setUp() {
        scope = TestScope(dispatcher)
        myRepository = MyRepository(dispatcher, scope)
    }

    @Test
    fun testMyRepository1() = scope.runTest {

        withContext(Dispatchers.IO){
            myRepository.fetchData(-1)
        }
    }
    @Test
    fun testMyRepository2() = scope.runTest {

        withContext(Dispatchers.IO){
            myRepository.fetchData(1)
        }
    }
}

Additionally, if I change the object of

myRepository to myRepository=mockk (relaxed=true)

, the coverage will be 0% and no code will be executed.


Another example is:

kotlin code:

class TestSample @Inject constructor(
    @DefaultDispatcher private val dispatcher: CoroutineDispatcher,
    @ApplicationScope private val scope: CoroutineScope,
    val userRepository: TestUserRepository
) {
    suspend fun fetchData(): String {
        println(11)
        val ret = userRepository.getCurrentUser()
        println(22)
        return ret
    }
}


class TestUserRepository @Inject constructor(
    @DefaultDispatcher private val dispatcher: CoroutineDispatcher,
    @ApplicationScope private val scope: CoroutineScope,
){
    suspend fun getCurrentUser():String{
        delay(100)
        return ""
    }
}

UT code:

class TestSampleTest {

    private lateinit var testSample: TestSample
    private lateinit var userRepository: TestUserRepository
    private val dispatcher = StandardTestDispatcher()
    private lateinit var scope: CoroutineScope

    @Before
    fun setUp() {
        userRepository = mockk(relaxed = true)
        scope = TestScope(dispatcher)
        scope = CoroutineScope(dispatcher + SupervisorJob())
        testSample = TestSample(dispatcher, scope, userRepository)
    }

    @After
    fun tearDown() {
        scope.cancel()
    }

    @Test
    fun test() = runBlocking {
        val expectResult = "result"
        coEvery { userRepository.getCurrentUser() }.returns(expectResult)

        val actualResult = testSample.fetchData()

        assertEquals(actualResult, expectResult)
    }
}

userRepository.getCurrentUser() cannot be coverage with 100%.


Another example is: Code:

class TestSample2 {
    suspend fun fetchData(): String {
        delay(5000)
        return "result"
    }
}

UT Code:

class TestSample2Test {

    private lateinit var testSample2: TestSample2

    @Before
    fun setUp() {
        testSample2 = TestSample2()
    }

    @Test
    fun test() = runTest  {
        val expectResult = "result"
        val actualResult = testSample2.fetchData()
        assertEquals(actualResult, expectResult)
    }
}

发布评论

评论列表(0)

  1. 暂无评论