DaggerMock
DaggerMock copied to clipboard
How to get test tear down in extended DaggerMockRule ?
trafficstars
Is it possible to get a method callback to the customized DaggerMockRule? (Usage : I want to close the mocked db)
Current implementation
@get:Rule
val mockDbRule = DaggerMockDbRule()
@After
fun tearDown() {
mockDbRule.db.close()
}
DaggerMockDbRule.kt
class DaggerMockDbRule : DaggerMockRule<AppComponent>(AppComponent::class.java, DatabaseModule()) {
var db: AppDatabase = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
AppDatabase::class.java
).build()
private val app =
InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as App
init {
customizeBuilder<DaggerAppComponent.Builder> {
it.baseNetworkModule(BaseNetworkModule(App.BASE_URL))
.contextModule(ContextModule(app))
}
set {
it.inject(app)
}
provides(AppDatabase::class.java, db)
}
}
Right now there aren't any callbacks, however if you need it you can try to override the apply method and add your call at the end of the Statement (after invoking base.evaluate())
Okay. I'll give it a try and update here.