我正在尝试进行一些测试,我需要通过在 KODEIN 上覆盖它来替换真正的依赖项,但它不起作用,我不知道我还能做什么。
这是我的依赖关系图(我省略了其他依赖关系):
class Injector(private val context: Context) {
val dependencies = Kodein.lazy {
.
.
bind<RetrieveContacts>() with provider {
Log.i("RetrieveContacts","REAL")
RetrieveContactsInMemory()
}
.
.
}
}
这是我的应用程序类:
class AppApplication : Application(), KodeinAware {
override val kodein by Injector(this).dependencies
}
这是我为覆盖依赖项所做的工作:
@RunWith(value = AndroidJUnit4::class)
class HomeActivityEmptyStateTest {
@get:Rule
var mActivityTestRule = ActivityTestRule<HomeActivity>(HomeActivity::class.java)
@Before
fun setup() {
val appApplication = InstrumentationRegistry.getInstrumentation().targetContext
val kodein by Kodein.lazy {
extend(appApplication.appKodein())
bind<RetrieveContacts>(overrides = true) with provider {
Log.i("RetrieveContacts","FAKE")
RetrieveEmptyContacts()
}
}
}
@Test
fun testWhenHasNoContent() {
...
}
}
我仍然在控制台日志中看到“RetrieveContacts REAL”而不是“RetrieveContacts FAKE”