0

Unit test case failed with the following log:

no answer found for: Context(child of object RuntimeContext#20).getApplicationContext()
io.mockk.MockKException: no answer found for: Context(child of object RuntimeContext#20).getApplicationContext()
    at app//io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:93)
    at app//io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
    at app//io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
    at app//io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
    at app//io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:266)
    at app//io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:23)
    at app//io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
    at app//io.mockk.proxy.jvm.advice.BaseAdvice.handle(BaseAdvice.kt:42)
    at app//io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.interceptNoSuper(JvmMockKProxyInterceptor.java:45)
    at app//android.content.Context$Subclass0.getApplicationContext(Unknown Source)
    at app//androidx.localbroadcastmanager.content.LocalBroadcastManager.getInstance(Unknown Source)

Here is the code which needs unit test:

    suspend operator fun invoke(): Boolean {
            val intent = Intent(Constants.ACTION_VTS_BOOTSTRAP_FINISHED)
            LocalBroadcastManager.getInstance(applicationContext).sendBroadcast(intent)
            WorkManager.getInstance(applicationContext).cancelUniqueWork(VtsPreparationWorker.TAG)
object RuntimeContext {
    lateinit var applicationContext: Context
    lateinit var environmentConfiguration: EnvironmentConfiguration
    lateinit var httpConfiguration: HttpConfiguration
    lateinit var appName: String
    lateinit var hceServiceName: String
    lateinit var appDeviceUserId: String
    lateinit var buildFlavorType: BuildFlavorType
    var dmsClient: IDMSClient? = null
    lateinit var mepCryptoClient: CryptoClient
    var calLoggingMgr: ICalLoggingManager? = null
        private set

    fun init(
        environmentConfiguration: EnvironmentConfiguration,
        httpConfig: HttpConfiguration,
        applicationContext: Context,
        appName: String,
        hceServiceName: String,
        appDeviceUserId: String,
        buildFlavorType: BuildFlavorType,
        dmsClient: IDMSClient?,
        mepCryptoClient: CryptoClient,
        calLoggingMgr: ICalLoggingManager?
    ) 

This is the unit test code I wrote:

        applicationContextMock = mockk(relaxed = true)
        environmentConfigMock = EnvironmentConfiguration(
            "baseUrl",
            "appVersion",
            "vAppId",
            "externalAppId",
            "packageName",
            "sbox",
            "keyAgreementJson",
            "keyAuthenticityJson",
            "softwareId",
            "softwareVersion",
            "productCode",
            "apiProductPath",
            "apiAuthPath",
            "correlationId",
            "dfpSessionId",
            "cryptoClientId"
        )
        httpConfigurationMock = mockk(relaxed = true)

        mockkObject(RuntimeContext)
        RuntimeContext.init(
            environmentConfigMock,
            httpConfigurationMock,
            applicationContextMock,
            "appName",
            "hceServiceName",
            "appDeviceUserId",
            mockk(),
            mockk(),
            mockk(),
            mockk(),
        )

        mockkConstructor(Intent::class)
        coEvery { anyConstructed<Intent>().putExtra(any(), any<String>()) } returns mockk()

        mockkConstructor(LocalBroadcastManager::class)
        coEvery {
            anyConstructed<LocalBroadcastManager>().sendBroadcast(any<Intent>())
        } returns true
Francis Bacon
  • 4,080
  • 1
  • 37
  • 48

0 Answers0