How to test PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS?
Asked Answered
H

2

9

In order to test the recurring work, I need to wait for a whole 15 minutes (reference) to test the recurrence and this is really not efficient. Is there a better and quicker way for developers to test this sooner?

Hadrian answered 27/8, 2018 at 12:37 Comment(2)
Isolate the code that actually does the work from the WorkManager-related classes. Write unit test cases or instrumentation test cases for that code.Wag
You can check here: developer.android.com/topic/libraries/architecture/workmanager/… the WorkManager's work-testing artifactRomie
G
3

You can test PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS which is 15 minutes.

For this, you need WorkManagerTestInitHelper available in androidx.work.testing.

First of all add the following dependency in the build.gradle file for your app or module:

//Current stable release is 2.3.4
androidTestImplementation "androidx.work:work-testing:2.3.4

Next, you need to use setPeriodDelayMet method available with TestDriver which can be used to indicate that an interval is complete and executes PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS (15 minutes). Sample code:

@Test
public void testPeriodicWork(Context context) throws Exception {
    // Setup input data
    Data input = new Data.Builder().put(KEY_1, 1).put(KEY_2, 2).build();

    // Create periodic work request
    PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(MyWorker.class, 15,  TimeUnit.MINUTES)
                                       .setInputData(input)
                                       .build();
    // Enqueue periodic request
    WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, request);

    // Initialize testDriver
    TestDriver testDriver = WorkManagerTestInitHelper.getTestDriver();

    // Tells the testing framework the period delay is met, this will execute your code in doWork() in MyWorker class
    testDriver.setPeriodDelayMet(request.getId());

}

You can find more information about testing PeriodicWorkRequest at https://developer.android.com/topic/libraries/architecture/workmanager/how-to/integration-testing#periodic-work

Learn more about testing WorkManager at https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper and https://developer.android.com/reference/androidx/work/testing/TestDriver

Gruber answered 4/5, 2020 at 7:9 Comment(2)
Probably obvious to most, but this will crash if you run it in a normal app, you'll get a androidx.work.impl.WorkManagerImpl cannot be cast to androidx.work.testing.TestWorkManagerImpl crashBecome
Answer is intended for TESTING purpose only (as the question was to TEST your logic without wait), When in test environment, you can trigger PeriodicWorkRequest without waiting. Always worked in our test cases, not sure about production, never checked through.Gruber
B
0

If I'm understanding well your problem, you need to test your Woker's work, no whether it's or not periodic, then you can use a OneTimeWorkRequest

val work = OneTimeWorkRequest.Builder(TheWorker::class.java).build()
WorkManager.getInstance().enqueue(work)
Bakki answered 4/2, 2019 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.