Thursday, 13 October 2022

How to get list of registered receivers in a Robolectric test?

I simply want to test that a specific broad cast receiver MyReceiver defined in the manifest is successfully registered, using a Robolectric test. Is this even possible?

I've tried this, but there are zero receivers in the list.

    @Test
    fun testBroadcastReceiverRegistered() {

        val context: Application = ApplicationProvider.getApplicationContext()
        val shadowApplication: ShadowApplication = Shadows.shadowOf(context)
        val registeredReceivers = shadowApplication.registeredReceivers
        assertFalse(registeredReceivers.isEmpty())

This also returns zero receivers

    @Test
    fun testBroadcastReceiverRegistered() {

        val rs = context.packageManager.queryBroadcastReceivers(
            Intent(context, MyReceiver::class.java),
            PackageManager.GET_RECEIVERS
        )
        assertFalse(rs.isEmpty())


from How to get list of registered receivers in a Robolectric test?

No comments:

Post a Comment