Skip to content

How to get test tear down in extended DaggerMockRule ? #103

@theapache64

Description

@theapache64

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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions