Код Kotlin:
Код: Выделить всё
class MyRepository @Inject constructor(
@DefaultDispatcher private val dispatcher: CoroutineDispatcher,
@ApplicationScope private val scope: CoroutineScope,
) {
private val checkService:ICheckService
init {
checkService = CheckService()
}
fun fetchData(): String {
return runBlocking(dispatcher) {
suspendCoroutine { continuation ->
println(111)
scope.launch(dispatcher) {
println(222)
delay(100)
println(333)
if(checkService.check()){
println(444)
}
continuation.resume("result")
}
}
}
}
}
interface ICheckService {
suspend fun check(): Boolean
}
class CheckService:ICheckService{
override suspend fun check(): Boolean {
return true
}
}
Код: Выделить всё
class MyRepositoryTest {
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 testMyRepository() = scope.runTest {
withContext([Dispatchers.IO](http://dispatchers.io/)){
myRepository.fetchData()
}
}
}
Код: Выделить всё
myRepository to myRepository=mockk (relaxed=true)
Подробнее здесь: https://stackoverflow.com/questions/792 ... -reach-100