lundi 3 décembre 2018

Nested mocks with kotlin mockito and coroutines

Hi I am trying to mock a KMongo collection, however I pass in the MongoDatabase and the repository gets it's collection. I find that cleaner.

I am trying to create a database mock, which returns a collection mock:

    @RunWith(MockitoJUnitRunner::class)
class UserRepositoryTest {
    @Mock
    lateinit var userCollectionMock: MongoCollection<User>
    @Mock
    lateinit var database: MongoDatabase

    @Test
    fun isInsertOneCalledTest() {
        runBlocking {
            whenever(userCollectionMock.insertOne(any<User>(), any<InsertOneOptions>())).thenAnswer { null }
            whenever(database.getCollection(any())).thenAnswer { userCollectionMock }

            val userRepository = UserMongoRepository(database)

            val testUser = User(UUID.randomUUID().toString(), "test@outlook.com")

            userRepository.createUser(testUser)

            verify(userCollectionMock.insertOne(any<User>(), any<InsertOneOptions>()))
        }
    }
}

It gives this error:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 3 matchers expected, 2 recorded: -> at com.bkaraargirov.users.UserRepositoryTest$isInsertOneCalledTest$1.invokeSuspend(UserRepositoryTest.kt:48) -> at com.bkaraargirov.users.UserRepositoryTest$isInsertOneCalledTest$1.invokeSuspend(UserRepositoryTest.kt:50)

I can read the error - the arguments seem fine. It blows up on the first whenever(). On that mock I return null since the return type is: Void?

Aucun commentaire:

Enregistrer un commentaire