Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The branch coverage of the suspend method cannot reach 100% #2316

Open
gkj17 opened this issue Dec 3, 2024 · 1 comment
Open

The branch coverage of the suspend method cannot reach 100% #2316

gkj17 opened this issue Dec 3, 2024 · 1 comment

Comments

@gkj17
Copy link

gkj17 commented Dec 3, 2024

Description

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?

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.

Steps to Reproduce

Expected Results

Actual Results

AndroidX Test and Android OS Versions

Link to a public git repo demonstrating the problem:

@gkj17
Copy link
Author

gkj17 commented Dec 3, 2024

3K494IDl
4hBISWzL
rEByrVMk
WpE8vcwX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant