I'm writing some unit tests for my defined LocalDataSource classes that wraps the functionality of a Room databse DAO, my code looks like this:
Room DAO Interface
@Dao
interface PersonDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(person: Person)
}
LocalDataSource Class
class PersonLocalDataSourceImpl(private val personDao: PersonDao) {
suspend fun insert(dispatcher: CoroutineDispatcher, person: Person) =
withContext(dispatcher) {
personDao.insert(person) // line 20
}
}
Unit Test Class
@ExperimentalCoroutinesApi
@RunWith(JUnit4::class)
class PersonLocalDataSourceTest : BaseLocalDataSourceTest() {
@Test
fun givenPersonLocalDataSource_WhenInsertPerson_ThenPersonDaoInsertFunctionCalledOnce() =
runBlockingTest {
withContext(testCoroutineDispatcher) {
val personDao = Mockito.mock(PersonDao::class.java)
val personLocalDataSource = PersonLocalDataSourceImpl(personDao)
val person = mockPerson()
personLocalDataSource.insert(testCoroutineDispatcher, person)
Mockito.verify(personDao).insert(person) // line 36
}
}
}
I'm getting this error when running the test:
Argument(s) are different! Wanted:
personDao.insert( Person( id = ...) ),
Continuation at (my package).PersonLocalDataSourceTest$givenPersonLocalDataSource_WhenInsertPerson_ThenPersonDaoInsertFunctionCalledOnce$1$1.invokeSuspend(PersonLocalDataSourceTest.kt:37)
Actual invocation has different arguments:
personDao.insert(Person( id = ...),
Continuation at (my package).PersonLocalDataSourceImpl$insert$2.invokeSuspend(PersonLocalDataSourceImpl.kt:20)
P.S. the test passes when I change the definition of the function PersonLocalDataSourceImpl::insert like follows:
override suspend fun insert(dispatcher: CoroutineDispatcher, person: Person) =
personDao.insert(person)