简体   繁体   中英

How to test Android Workers with In-Memory Room database injected by Koin

My current Android Application employs Koin for DI

def koin_version = '2.0.1'
api "org.koin:koin-androidx-scope:$koin_version"
api "org.koin:koin-androidx-viewmodel:$koin_version"
api "org.koin:koin-androidx-ext:$koin_version"

androidTestImplementation "org.koin:koin-test:$koin_version"

I wish to test my Workers that have my Room database injected into them

For testing I wish to inject an In Memory version of my Room database.

the worker version I have is

def work_version = '2.3.0'
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-runtime-ktx:$work_version"
androidTestImplementation "androidx.work:work-testing:$work_version"

This is the test I am running

@RunWith(AndroidJUnit4::class)
class MyWorkerTest : KoinTest {
    private lateinit var context: Context

    @Before
    fun startKoinForTest() {
        context = InstrumentationRegistry.getInstrumentation().targetContext

        if (GlobalContext.getOrNull() == null) {
            startKoin {
                androidLogger()
                androidContext(context)
                modules(roomTestModule)
            }
        }
    }

    @After
    fun stopKoinAfterTest() = stopKoin()

    @Test
    fun testMyWorker() {
        val worker = TestListenableWorkerBuilder<MyWorker>(context).build()
        runBlocking {
            val result = worker.doWork()
            assertThat(result, `is`(Result.success()))
        }
    }
}

My Koin testDatabase Module resembles this :-

val roomTestModule = module {
    single {
        Room.inMemoryDatabaseBuilder(androidApplication(), MyDatabase::class.java)
            .allowMainThreadQueries()
            .addCallback(object : RoomDatabase.Callback() {
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)
                    Timber.e("In Memory")
                }
            })
            .build()
    }
}

However the Database version injected into my Worker is always the Disk based one.

My Worker resembles this :-

class MyWorker(val context: Context, val params: WorkerParameters) :
    CoroutineWorker(context, params),
    KoinComponent {

    internal val service: MyService by inject()
    internal val database: MyDatabase by inject()

    override suspend fun doWork(): Result = coroutineScope {
        withContext(Dispatchers.IO) {


        }
    }
}

How can I inject my In Memory database into my workers for testing?

You can use a custom WorkerFactory ( https://developer.android.com/reference/androidx/work/WorkerFactory ) to create instances of your Workers. That way you can have Koin inject the dependencies before doWork() is invoked.

To use a custom WorkerFactory you need to use the following API: https://developer.android.com/reference/androidx/work/Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM