I have the following class I am trying to test. I am using mockito as that is the library I am using
class ConsentUpdatedBroadcastReceiverImp @Inject constructor(
private val context: Context,
private val contentManager: ContentManager,
private val applicationScope: CoroutineScope,
) : ConsentUpdatedBroadcastReceiver {
override val observe: Flow<ConsentStatusPreference>
get() {
return callbackFlow {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val consentStatusPreference = contentManager.getConsentStatusPreference()
trySend(consentStatusPreference).isSuccess
}
}
ContextCompat.registerReceiver(
context,
receiver,
IntentFilter(BroadcastServiceKeys.CONSENT_UPDATED),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
awaitClose {
context.unregisterReceiver(receiver)
}
}.shareIn(
scope = applicationScope,
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 5_000L),
)
}
}
This is my unit test
class ConsentUpdatedBroadcastReceiverImpTest {
private val ontentManager = mock<ContentManager>()
private val testDispatcher = StandardTestDispatcher()
private val applicationScope = TestScope(testDispatcher)
private val context = mock<Context>()
private lateinit var consentUpdatedBroadcastReceiverImp: consentUpdatedBroadcastReceiverImp
@Before
fun setUp() {
consentUpdatedBroadcastReceiverImp = ConsentUpdatedBroadcastReceiverImp(
context = context,
contentManager = contentManager,
applicationScope = applicationScope
)
}
@Test
fun `given after receiving a broadcast then send broadcast`() {
runTest {
// Given
val consentStatusPreference = mock<ConsentStatusPreference> {
on { performance } doReturn ConsentStatusType.DENIED
on { functionality } doReturn ConsentStatusType.ACCEPTED
on { targeting } doReturn ConsentStatusType.NOT_COLLECTED
}
whenever(contentManager.getConsentStatusPreference()).thenReturn(consentStatusPreference)
var registeredReceiver: BroadcastReceiver? = null
whenever(context.registerReceiver(any(), any(), any(), anyOrNull())).thenAnswer {
registeredReceiver = it.getArgument(1)
null
}
// When
consentUpdatedBroadcastReceiverImp.observe.test {
registeredReceiver?.onReceive(context, Intent(OTBroadcastServiceKeys.OT_CONSENT_UPDATED))
val _consentStatusPreference = this.awaitItem()
// Then
assert(_consentStatusPreference === consentStatusPreference)
cancelAndIgnoreRemainingEvents()
}
}
}
}
This is the error when running the test
No value produced in 3s
app.cash.turbine.TurbineAssertionError: No value produced in 3s
at app//app.cash.turbine.ChannelKt.awaitEvent(channel.kt:89)
at app//app.cash.turbine.ChannelKt$awaitEvent$1.invokeSuspend(channel.kt)
at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at app//kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
at app//kotlinx.coroutines.test.TestDispatcher.processEvent$kotlinx_coroutines_test(TestDispatcher.kt:24)
at app//kotlinx.coroutines.test.TestCoroutineScheduler.tryRunNextTaskUnless$kotlinx_coroutines_test(TestCoroutineScheduler.kt:99)
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$workRunner$1.invokeSuspend(TestBuilders.kt:322)
at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)