You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
The text was updated successfully, but these errors were encountered:
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:
UT code:
Additionally, if I change the object of
, 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:
The text was updated successfully, but these errors were encountered: