Android WorkManager has error when testing with Robolectric
Asked Answered
H

1

11

I'm using Android work manager with a custom initialization. to do that i disable auto-initialization in the manifest like this

        <provider
        tools:replace="android:authorities"
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.work_manager_init"
        android:enabled="false"
        android:exported="false" />

And in application code I use this code

private fun initWorkManager() {
    WorkManager.initialize(this, Configuration.Builder().run {
        setWorkerFactory(appComponent.daggerWorkerFactory())
        build()
    })
}

And it works fine when I run my application. But when I'm testing with roboletric any class that needs the context by RuntimeEnvironment.application throws this exception

java.lang.IllegalStateException: WorkManager is already initialized.  
Did you try to initialize it manually without disabling 
WorkManagerInitializer? See WorkManager#initialize(Context, 
Configuration) or the class levelJavadoc for more information.

The initWorkManager() get calls and throw this beacuse it doesn't know auto-init is already disabled in the manifest and somehow my test cannot read the values from the manifest file.

Any help or suggestion will be appritiated.

Haily answered 8/1, 2019 at 7:38 Comment(4)
Which version of Robolectric are you using? Did you specify a special manifest file to be used by your tests? Did you maybe override the manifest in your test/ directory? You could also try to only initialize Workmanager by yourself if it is NOT a robolectric test.Fray
I have this problem also, except my application does not require a custom initialization. If I manually call WorkManager.initialize() as above I get the same error. If I don't call initialize(), I instead get ```java.lang.IllegalStateException: WorkManager is not initialized properly. The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.`` It seems like there's not a good way out of this. It complains whether or not initialize() is called. Catch 22.Sheaves
I have exactly same problem. I disabled the default inicialization via Manifest and it works when I run the app normally but when I run the Roboelectric test it fails with the error you mentioned.Branscum
Anyone found a solution/workaround?Cattery
B
11

I solved a similar issue with the help of the Androidx Work Manager testing utils. For reference see the docs here: Android work manager testing docs

Basically what you need to do is add a dependency to include the work manger test utils:

testImplementation 'androidx.work:work-testing:2.0.1'

Then you will be able to call code in your test setup similar to this:

final Configuration config = new Configuration.Builder()
    .setMinimumLoggingLevel(Log.DEBUG)
    .setExecutor(new SynchronousExecutor())
    .build();
WorkManagerTestInitHelper.initializeTestWorkManager(
    context, config);

Whereas the context could obtained in different ways, depending on your test infrastructure.

With this approach no other steps like excluding something from the manifest are necessary.

Blabbermouth answered 15/5, 2019 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.