Does Koin work with Android BroadcastReceiver in the manifest?
See original GitHub issueDescribe the bug
I have an Android app which uses Koin and which uses Robolectric for some unit tests.
All unit tests that use Robolectric fail with the stack below.
This appears to be because I have a BroadcastReceiver declared in the manifest
<receiver android:name="MyBroadcastReceiver"/>
And MyBroadcastReceiver implements KoinComponent and uses by inject
.
It appears that because the BroadcastReceiver is declared in the manifest it gets created BEFORE my App.onCreate (which is where I start Koin). When I remove the BroadcastReceiver from the manifest the tests pass.
Am I doing something wrong? Possibly this is a Robolectric bug instead? I am migrating an app from Dagger to Koin and these unit test previously worked fine with Dagger.
java.lang.IllegalStateException: KoinApplication has not been started
at org.koin.core.context.GlobalContext.get(GlobalContext.kt:37)
at org.koin.core.KoinComponent$DefaultImpls.getKoin(KoinComponent.kt:32)
at com.metaswitch.max.util.MyBroadcastReceiver.getKoin(MyBroadcastReceiver.kt:52)
at com.metaswitch.max.util.MyBroadcastReceiver.<init>(MyBroadcastReceiver.kt:143)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:379)
at org.robolectric.internal.bytecode.ShadowImpl.newInstanceOf(ShadowImpl.java:18)
at org.robolectric.shadow.api.Shadow.newInstanceOf(Shadow.java:35)
at org.robolectric.android.internal.AndroidEnvironment.registerBroadcastReceivers(AndroidEnvironment.java:491)
at org.robolectric.android.internal.AndroidEnvironment.installAndCreateApplication(AndroidEnvironment.java:242)
at org.robolectric.android.internal.AndroidEnvironment.setUpApplicationState(AndroidEnvironment.java:149)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:298)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:247)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
To Reproduce See above.
Expected behavior Unit test to pass.
Koin project used and used version (please complete the following information): “org.koin:koin-androidx-viewmodel:2.0.1” “org.koin:koin-test:2.0.1”
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
I think I see how to workaround this.
Split my BroadcastReceiver into two classes:
Now MyHelper isn’t created until Koin has been initialised in App.onCreate.
check for #237