Search code examples
androidandroid-architecture-componentsandroid-workmanager

WorkManager: Call doWork() immediately for test purposes


If I have a PeriodicWorkRequest I can set a time intervall - which minimum is 15 minutes. But how can I test if my doWork() method is working without waiting for 15 minutes?

Is it maybe possible to use OneTimeWorkRequest for test purposes?

Thanks in advance


Solution

  • Yes, there is nothing stopping you from using OneTimeWorkRequest.

    val work = OneTimeWorkRequest.Builder(MyWorker::class.java).build()
    WorkManager.getInstance().enqueue(work)
    

    Bear in mind, that this will not allow you to test that your Worker will fire after a set number of minutes, like when using PeriodicWorkRequest. It will however let you test that your code works, without needing to wait.