I know that WorkManager provides a work-testing artifact for test workers and we can use TestListenableWorkerBuilder to test CoroutineWorker (see this link for more information). I found an medium article by Ian Roberts showing how to test CoroutineWorker with dependencies by creating your own WorkerFactory.
According to official documentation, we can test initial delays on Worker using TestDriver but nothing was said about testing delays, constraints etc on CoroutinesWork. Is there a way to perform such tests in CoroutineWorker using TestListenableWorkerBuilder?
After watching this video (at 13:00) from the 2019 Android Dev Summit, I found the answer for this question:
When initializing
workManagerfor test (byWorkManagerTestInitHelper.initializeTestWorkManagermethod), we have to pass our customWorkerFactorythrough the configuration step;Setup your request worker as you normally would using the
OneTimeWorkRequestBuildermethod;By default, all constraints for
Workmanagerinstances in the test mode are unmet. Using an instance ofTestDriver, we can mark those constraints as met.Here's an example to summarize these above steps: