Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContextCompat.registerReceiver() throws DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION error for Robolectric tests on SDK 32 and lower #9124

Open
bjenning04 opened this issue May 31, 2024 · 0 comments

Comments

@bjenning04
Copy link

Description

When a unit test using Robolectric with SDK 32 and lower executes some code that is using ContextCompat.registerReceiver(), it erroneously throws the following exception:

Permission com.cerner.careaware.connect.sample.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION is required by your application to receive broadcasts, please add it to your manifest
java.lang.RuntimeException: Permission com.cerner.careaware.connect.sample.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION is required by your application to receive broadcasts, please add it to your manifest
	at androidx.core.content.ContextCompat.obtainAndCheckReceiverPermission(ContextCompat.java:970)
	at androidx.core.content.ContextCompat$Api26Impl.registerReceiver(ContextCompat.java:1117)
	at androidx.core.content.ContextCompat.registerReceiver(ContextCompat.java:822)
	at androidx.core.content.ContextCompat.registerReceiver(ContextCompat.java:769)
	at com.cerner.careaware.connect.sample.MainActivity.onCreate(MainActivity.kt:42)
	at android.app.Activity.performCreate(Activity.java:8054)
	at android.app.Activity.performCreate(Activity.java:8034)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
	at org.robolectric.android.internal.RoboMonitoringInstrumentation.callActivityOnCreate(RoboMonitoringInstrumentation.java:382)
	at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:138)
	at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:243)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:136)
	at org.robolectric.android.controller.ActivityController.create(ActivityController.java:146)
	at org.robolectric.android.internal.RoboMonitoringInstrumentation.lambda$startActivitySyncInternal$0(RoboMonitoringInstrumentation.java:130)
	at org.robolectric.shadows.ShadowInstrumentation.runOnMainSyncNoIdle(ShadowInstrumentation.java:1190)
	at org.robolectric.android.internal.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:125)
	at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:36)
	at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:41)
	at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:362)
	at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:202)
	at com.cerner.careaware.connect.sample.MainActivityTest.testApi32AndLower(MainActivityTest.kt:16)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:489)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$2(SandboxTestRunner.java:291)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:104)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)

Steps to Reproduce

Using this sample project, execute these unit tests. This test for API 33 and higher passes, while this test for 32 and lower fails with the above exception.

Robolectric & Android Version

Robolectric version 4.12.2
Tested with Android SDK 26-34. Tests pass with SDK 33 and 34, fail with 32 and lower.

Link to a public git repo demonstrating the problem:

https://github.com/bjenning04/robolectric-permission-sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant