i am trying to mock a repository class using declareMock
and mockk
but it doesn't seem to be working as i am getting the data from the real repository.
Version
implementation "org.koin:koin-androidx-viewmodel:2.1.6"
testImplementation "org.koin:koin-test:2.1.6"
Code
My application class (only relevant parts):
class MyApplication : Application() {
companion object {
val appModule = module {
single<RepositoryUserLists> { RepositoryUserListsImpl() }
}
}
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@MyApplication)
modules(appModule)
}
}
}
My test class (only relevant parts)
class TestRepositoryUserLists : KoinTest {
@get:Rule
val koinTestRule = KoinTestRule.create {
modules(MyApplication.appModule)
}
@get:Rule
val mockProvider = MockProviderRule.create { clazz ->
mockkClass(clazz.java.kotlin)
}
@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()
private val repo: RepositoryUserLists by inject()
@Before
fun before() {
declareMock<RepositoryUserLists> {
every { getAllLists() } returns MutableLiveData(listOf(MyList("test list")))
}
//PROBLEM IS HERE
//Expected: a list containing one item names "test list".
//Actual: empty list (like in real repository).
repo.getAllLists().value
}
}
github issue: https://github.com/InsertKoinIO/koin/issues/841