We will cover briefly:

  1. Custom WorkManager
  2. OneTimeWorkRequest
  3. PeriodicWorkRequest
  4. Write Tests for Workers (step2 and step3)

Custom WorkManager

There is detailed description here for WorkManager

As per the docs

By default, WorkManager configures itself automatically when your app starts. If you require more control of how WorkManager manages and schedules work, you can customise the WorkManager configuration.

  • Install the dependencies inside app.gradle 
def work_version = "2.5.0"
implementation "androidx.work:work-runtime-ktx:$work_version"
androidTestImplementation "androidx.work:work-testing:$work_version"
  • Remove the default initialiser from AndroidManifest.xml 
   tools:node="remove" />

Create your application class and define your own custom configuration.

class TodoApplication() : Application(), Configuration.Provider {
    override fun onCreate() {
    override fun getWorkManagerConfiguration(): Configuration {
      return if (BuildConfig.DEBUG) {
      else {

Here, we use Configuration.Provider to extend our TodoApplication, and override the getWorkManagerConfiguration

In our case, we just configure the logging level. For the complete list of customisations available, see the Configuration.Builder()


This is how our folder structure looks like

Workers in the App
Workers in the App

For creating any worker, we need the following:

  • Worker (this section) : Actual work you want to perform in the background
  • WorkRequest (created in the viewmodel section): This represents a request to do some work.
  • WorkManager (created above): Schedules your WorkRequest and makes it run

We will start with OnDemandBackupWorker, which basically aims to save the data onto some backend, (for our demo, we fake in the network call)

This is then followed by FileWorker, which creates a file onto the device and appends the timestamp into the newly created file

FileWorker Output
FileWorker Output


  • This class extends from the CoroutineWorker (provides interop with Kotlin Coroutines)
  • We override the doWork function for our suspending work
override suspend fun doWork(): Result {
    val appContext = applicationContext
    showNotifications("Backing up the data", appContext)
    return try {
       val res = dummyWork()
       val outputData = workDataOf(KEY_ONDEMANDWORKER_RESP to res)
    } catch (throwable: Throwable) {
private suspend fun dummyWork(): String {
     // Faking the network call
     return "Completed successfully!"

Here, we create a dummyWork function, (which puts the thread to sleep) and returns string result.

  • The result is then put inside workDataOf (this converts list of pairs to Data object) using a key (which should be a string)
  • This result/output is then passed onto Worker’s Result.success that indicates the work completed successfully
  • In case of any error, we call Worker’s Result.failure 


  • This class extends from the Worker (performs work synchronously on a background thread)
  • We override the doWork function for our synchronous work
override fun doWork(): Result {
return try {
   val content="Backed up on ${dateFormatter.format(Date())}"
   val outputUri = saveToFile(appContext, content)
   val data=workDataOf(KEY_FILEWORKER_RESP to outputUri.toString())
 } catch (throwable: Throwable) {

Here, we create a file using saveToFile, and put in the current timestamp as text into that file

  • The result is then put inside workDataOf and like before, we pass it into the Worker’s Result.success


  • This class is responsible for creating the work request and extends from the AndroidViewModel.
  • We define two functions (beginBackup and cancelBackup) inside this class
internal fun beginBackup() {
   val constraints = Constraints.Builder()
var continuation = workManager.beginUniqueWork(
   val backupBuilder = OneTimeWorkRequestBuilder<OnDemandBackupWorker>()
  continuation = continuation.then(backupBuilder.build())
  val saveInFile = OneTimeWorkRequest.Builder(FileWorker::class.java)
  continuation = continuation.then(saveInFile)

internal fun cancelBackup() {

  • Specify the constraints under which the worker is supposed to run, (in our case we specify storage, battery and internet)
  • We get an instance of workmanager inside our viewmodel class. Using this instance, we call the beginUniqueWork 

Since, we need to chain our work requests, hence we use beginUniqueWork

  • We give our worker a unique name (basically a string), specify the ExistingWorkPolicy with options asKEEP and create a OneTimeWorkRequest from OnDemandBackupWorker class
  • The output from beginUniqueWork is a WorkContinuation.
  • Next, we create the WorkRequest Builder, using OneTimeWorkRequestBuilder . We add the tag for this work, which is used to later identify the progress for this work
  • This request is added to the WorkContinuation 
  • We repeat the last two steps for our FileWorker
  • Finally, our chain of work request chain needs to be added to the queue (in order to run on the background thread). This is done using enqueue 

Note: For cancelling a work request, we simply call the cancelUniqueWork with the tag (used for creating the work) 

Tracking Progress of WorkRequest

  • Since we added tags for our Worker, we can utilise them to get the status of any WorkRequest 
  • It returns a LiveData that holds a WorkInfo object. WorkInfo is an object that contains details about the current state of a WorkRequest 
internal val backupDataInfo: LiveData<List<WorkInfo>> = workManager

Here, TAG_BACKUP is our tag, specified previously


Note: PeriodicBackupWorker is the same like FileWorker (only difference is the file content)

  • This work executes multiple times until it is cancelled, with the first execution happening immediately or as soon as the given Constraints are met.
  • The next execution will happen during the period interval.
  • Go to our Application class and instantiate the PeriodicWorkRequest.Builder
val constraints = Constraints.Builder()
val periodicBackup = PeriodicWorkRequestBuilder<PeriodicBackupWorker>(1, TimeUnit.DAYS)

  • We specify the constraints as needed
  • Using PeriodicWorkRequestBuilder, we create our PeriodicWorkRequest 
  • The time interval is specified as once per day.

Periodic work has a minimum interval of 15 minutes. Also if your periodic work has constraints, it will not execute until the constraints are met, even if the delay between periods has been met.

  • We enqueue our PeriodicWorkRequest using enqueueUniquePeriodicWork, keeping the work policy as Keep 

The normal lifecycle of a PeriodicWorkRequest is ENQUEUED -> RUNNING -> ENQUEUED

Write Tests for Workers

We will now write tests for our workers: OnDemandBackupWorker and PeriodicBackupWorker 


  • Create a test class OnDemandBackupWorkerTest
  • We annotate our class with AndroidJUnit4 This is the thing that will drive the tests for a single class.
class OnDemandBackupWorkerTest {
 private lateinit var context: Context
 private lateinit var executor: Executor
 fun setUp() {
     context = ApplicationProvider.getApplicationContext()
     executor = Executors.newSingleThreadExecutor()
 fun testOnDemandBackupWorker() {
    val worker = TestListenableWorkerBuilder<OnDemandBackupWorker> (context).build()
   runBlocking {
     val result = worker.doWork()
     assertTrue(result is ListenableWorker.Result.Success)

  • We initialise the context and executor inside our setUp and write a single test testOnDemandBackupWorker 
  • Since, our OnDemandBackupWorker is a CoroutineWorker, we make use of TestListenableWorkerBuilder which basically builds instances of ListenableWorker used for testing.
  • Next, we call the doWork inside runBlocking, (it executes the test synchronously on the main thread, which we want for testing)


// TEST 1
fun testPeriodicBackUpWorker() {
   val worker = TestWorkerBuilder<PeriodicBackupWorker>(
        context = context,
        executor = executor
   val result = worker.doWork()
   assertTrue(result is ListenableWorker.Result.Success)

  • Since, our PeriodicBackupWorker is a Worker, we make use of TestWorkerBuilder which basically builds instances of Worker used for testing.
  • Next, we call the doWork and assert for the Result.success 

  • For the second test, we check if the periodic work state is ENQUEUED
// TEST 2
fun testIfPeriodicBackupRunning() {
 val testDriver = WorkManagerTestInitHelper.getTestDriver(context)
 val workManager = WorkManager.getInstance(context)
 val constraints = Constraints.Builder()
 val request =
     PeriodicWorkRequestBuilder<PeriodicBackupWorker> (repeatInterval=24, TimeUnit.HOURS)
 with(testDriver) {
 val workInfo = workManager.getWorkInfoById(request.id).get()
 assertEquals(workInfo.state, WorkInfo.State.ENQUEUED)
  • We make use of WorkManagerTestInitHelper which helps initialise WorkManager for testing.
  • Set the constraints, create a PeriodicWorkRequest and enqueue it using the workManager instance
  • Next, we make use of testDriver to make the constraints meet and assert if the status is ENQUEUED

Source Code

Valuable comments