André Martins
04/20/2022, 4:41 PMCoroutineCollection
with the following code
data class User(val id: Int, val name: String)
class Service(private val myDatabase: CoroutineDatabase) {
private val userCollection: CoroutineCollection<User> = myDatabase.getCollection("users")
suspend fun getById(id: Int): User? = userCollection.findOneById(id)
// ...
suspend fun insertUser(user: User): InsertOneResult = userCollection.insertOne(user)
}
@Test
fun myTest(): Unit = runBlocking {
// Arrange
val database = mockk<CoroutineDatabase>()
val userCol = mockk<CoroutineCollection<User>>()
every {
database.getCollection<User>("users")
} returns userCol
val service = Service(database)
val expectedUser = User(30, "Joe")
coEvery {
userCol.findOneById(30)
} returns expectedUser
// Act
val actualUser = service.getById(30)
// Assert
assertEquals(expectedUser, actualUser)
}
The error is
class org.litote.kmongo.coroutine.CoroutineCollection cannot be cast to class com.mongodb.reactivestreams.client.MongoCollection (org.litote.kmongo.coroutine.CoroutineCollection and com.mongodb.reactivestreams.client.MongoCollection are in unnamed module of loader 'app')
I suspect this is due to functions like CoroutineDatabase::getCollection<T>(String)
being inlineMattia Tommasone
04/20/2022, 8:23 PMAndré Martins
04/21/2022, 3:15 PM