Mocking of the code executed inside async {} block, goes to endless waiting.
the code under the test, where databaseClient.x.y.z should be mocked:
suspend fun findByFilter(statement: String, rowCount: String, pageable: Pageable): PageImpl<Geoitem> =
withContext(Dispatchers.IO) {
val geoItemsList = async {
databaseClient.sql(statement)
.map(rowMapper::apply)
.all()
.asFlow()
.buffer(bufferSize)
.toList()
}
val totalCount = async {
databaseClient.sql(rowCount)
.map(singleValueMapper::apply)
.all()
.awaitSingle()
}
}
the code in unit test:
@Test
fun `should execute SQL query and counting query in parallel`() = runTest {
// given
// THIS block causes endless loop
coEvery {
databaseClient.sql(pagedSql).map(rowMapper::apply)
.all().asFlow().buffer(100).toList()
} returns mockk<List<Geoitem>>()
// THIS block also causes endless loop
coEvery {
databaseClient.sql(countSql).map(singleValueMapper::apply)
.all()
.awaitSingle()
} returns 1L
// when
repository.findByFilter(sql, countSql, PageRequest.of(0, 20))
// then
coVerify(exactly = 3) { rowMapper.apply(any(), any()) }
coVerify(exactly = 1) { singleValueMapper.apply(any(), any()) }
}
any attempt to mock the code inside suspend function goes to endless loop. How should I do it properly?