diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HomeTracingRegisterDao.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HomeTracingRegisterDao.kt index 338862ea130..aa25131f95a 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HomeTracingRegisterDao.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HomeTracingRegisterDao.kt @@ -23,7 +23,7 @@ import org.hl7.fhir.r4.model.Coding import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry import org.smartregister.fhircore.engine.data.local.DefaultRepository import org.smartregister.fhircore.engine.data.local.tracing.TracingRepository -import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @Singleton @@ -34,7 +34,7 @@ constructor( tracingRepository: TracingRepository, defaultRepository: DefaultRepository, configurationRegistry: ConfigurationRegistry, - dispatcherProvider: DefaultDispatcherProvider, + dispatcherProvider: DispatcherProvider, sharedPreferencesHelper: SharedPreferencesHelper, ) : TracingRegisterDao( diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/PhoneTracingRegisterDao.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/PhoneTracingRegisterDao.kt index ea391424820..40e1464f618 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/PhoneTracingRegisterDao.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/PhoneTracingRegisterDao.kt @@ -23,7 +23,7 @@ import org.hl7.fhir.r4.model.Coding import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry import org.smartregister.fhircore.engine.data.local.DefaultRepository import org.smartregister.fhircore.engine.data.local.tracing.TracingRepository -import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @Singleton @@ -34,7 +34,7 @@ constructor( defaultRepository: DefaultRepository, tracingRepository: TracingRepository, configurationRegistry: ConfigurationRegistry, - dispatcherProvider: DefaultDispatcherProvider, + dispatcherProvider: DispatcherProvider, sharedPreferencesHelper: SharedPreferencesHelper, ) : TracingRegisterDao( diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDao.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDao.kt index 07a4cc1397d..9f16e2128e0 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDao.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDao.kt @@ -55,7 +55,7 @@ import org.smartregister.fhircore.engine.domain.model.ProfileData import org.smartregister.fhircore.engine.domain.model.RegisterData import org.smartregister.fhircore.engine.domain.repository.RegisterDao import org.smartregister.fhircore.engine.domain.util.PaginationConstant -import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.LOGGED_IN_PRACTITIONER import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.asDdMmmYyyy @@ -74,13 +74,12 @@ import org.smartregister.fhircore.engine.util.extension.safeSubList import org.smartregister.fhircore.engine.util.extension.toAgeDisplay import timber.log.Timber -abstract class TracingRegisterDao -constructor( +abstract class TracingRegisterDao( open val fhirEngine: FhirEngine, val defaultRepository: DefaultRepository, private val tracingRepository: TracingRepository, val configurationRegistry: ConfigurationRegistry, - val dispatcherProvider: DefaultDispatcherProvider, + val dispatcherProvider: DispatcherProvider, val sharedPreferencesHelper: SharedPreferencesHelper, ) : RegisterDao { diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/CoreModule.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/CoreModule.kt index 911cc434fa6..ca29e8e5e79 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/CoreModule.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/CoreModule.kt @@ -41,7 +41,7 @@ import org.smartregister.fhircore.engine.trace.PerformanceReporter import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @InstallIn(SingletonComponent::class) -@Module(includes = [NetworkModule::class, DispatcherModule::class]) +@Module class CoreModule { @Singleton diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt index 1d68df1212d..5eabba012dc 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt @@ -38,7 +38,7 @@ import org.smartregister.fhircore.engine.di.NetworkModule.Companion.TIMEOUT_DURA * Provide [FhirEngine] dependency in isolation so we can replace it with a fake dependency in test */ @InstallIn(SingletonComponent::class) -@Module(includes = [CoreModule::class]) +@Module class FhirEngineModule { @Singleton diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/InfoCard.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/InfoCard.kt index 80eff6e1a41..91998bb768e 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/InfoCard.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/InfoCard.kt @@ -47,14 +47,15 @@ import androidx.compose.ui.text.intl.Locale import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.lifecycle.LiveData import org.smartregister.fhircore.engine.domain.util.DataLoadState import org.smartregister.fhircore.engine.ui.theme.BlueTextColor import org.smartregister.fhircore.engine.ui.theme.LighterBlue import org.smartregister.fhircore.engine.util.annotation.ExcludeFromJacocoGeneratedReport @Composable -fun InfoCard(viewModel: SettingsViewModel) { - val state by viewModel.profileData.observeAsState() +fun InfoCard(profileData: LiveData>) { + val state by profileData.observeAsState() when (state) { is DataLoadState.Loading -> diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/SettingsScreen.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/SettingsScreen.kt index 35c96d71e7b..aff09145348 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/SettingsScreen.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/settings/SettingsScreen.kt @@ -110,7 +110,7 @@ fun SettingsScreen( .padding(vertical = 20.dp) .verticalScroll(rememberScrollState()), ) { - InfoCard(viewModel = settingsViewModel) + InfoCard(profileData = settingsViewModel.profileData) Divider(color = DividerColor) UserProfileRow( icon = Icons.Rounded.Download, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/app/TestDispatcher.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/app/TestDispatcher.kt new file mode 100644 index 00000000000..b9712a1a4ca --- /dev/null +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/app/TestDispatcher.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2021 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.engine.app + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher + +@OptIn(ExperimentalCoroutinesApi::class) val testDispatcher = UnconfinedTestDispatcher() diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/app/di/module/FakeDispatcherModule.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/app/di/module/FakeDispatcherModule.kt new file mode 100644 index 00000000000..23e6bb1c517 --- /dev/null +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/app/di/module/FakeDispatcherModule.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2021 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.engine.app.di.module + +import dagger.Module +import dagger.Provides +import dagger.hilt.components.SingletonComponent +import dagger.hilt.testing.TestInstallIn +import javax.inject.Singleton +import org.smartregister.fhircore.engine.app.testDispatcher +import org.smartregister.fhircore.engine.di.DispatcherModule +import org.smartregister.fhircore.engine.util.DispatcherProvider + +@Module +@TestInstallIn(components = [SingletonComponent::class], replaces = [DispatcherModule::class]) +class FakeDispatcherModule { + + @Provides + @Singleton + fun provideDispatcherProvider(): DispatcherProvider = + object : DispatcherProvider { + + override fun main() = testDispatcher + + override fun default() = testDispatcher + + override fun io() = testDispatcher + + override fun unconfined() = testDispatcher + } +} diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/auth/TokenAuthenticatorTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/auth/TokenAuthenticatorTest.kt index adeb9c4bf01..3ef54831f7b 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/auth/TokenAuthenticatorTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/auth/TokenAuthenticatorTest.kt @@ -54,6 +54,7 @@ import org.smartregister.fhircore.engine.data.remote.model.response.OAuthRespons import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SecureSharedPreference import org.smartregister.fhircore.engine.util.toPasswordHash import retrofit2.HttpException @@ -69,6 +70,8 @@ class TokenAuthenticatorTest : RobolectricTest() { @Inject lateinit var secureSharedPreference: SecureSharedPreference @Inject lateinit var configService: ConfigService + + @Inject lateinit var dispatcherProvider: DispatcherProvider private val oAuthService: OAuthService = mockk() private lateinit var tokenAuthenticator: TokenAuthenticator private val accountManager = mockk() @@ -85,7 +88,7 @@ class TokenAuthenticatorTest : RobolectricTest() { secureSharedPreference = secureSharedPreference, configService = configService, oAuthService = oAuthService, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, accountManager = accountManager, context = context, ), @@ -234,7 +237,7 @@ class TokenAuthenticatorTest : RobolectricTest() { secureSharedPreference = secureSharedPreference, configService = configService, oAuthService = oAuthService, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, accountManager = accountManager, context = context, ), @@ -428,7 +431,7 @@ class TokenAuthenticatorTest : RobolectricTest() { secureSharedPreference = secureSharedPreference, configService = configService, oAuthService = oAuthService, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, accountManager = accountManager, context = context, ), diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt index 11890e9c5b7..a83215b070c 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt @@ -27,6 +27,7 @@ import dagger.hilt.android.testing.HiltAndroidTest import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk +import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.StandardTestDispatcher @@ -46,6 +47,7 @@ import org.smartregister.fhircore.engine.configuration.view.PinViewConfiguration import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @HiltAndroidTest @@ -57,6 +59,8 @@ class ConfigurationRegistryTest : RobolectricTest() { val context = ApplicationProvider.getApplicationContext() @get:Rule(order = 1) val coroutineRule = CoroutineTestRule() + + @Inject lateinit var dispatcherProvider: DispatcherProvider private val testAppId = "default" private lateinit var fhirResourceDataSource: FhirResourceDataSource lateinit var configurationRegistry: ConfigurationRegistry @@ -75,7 +79,7 @@ class ConfigurationRegistryTest : RobolectricTest() { fhirEngine, fhirResourceDataSource, sharedPreferencesHelper, - coroutineRule.testDispatcherProvider, + dispatcherProvider, ) coEvery { fhirResourceDataSource.loadData(any()) } returns Bundle().apply { entry = mutableListOf() } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/DefaultRepositoryTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/DefaultRepositoryTest.kt index f4ecd4ae8f5..e918c269e7f 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/DefaultRepositoryTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/DefaultRepositoryTest.kt @@ -36,6 +36,7 @@ import io.mockk.slot import io.mockk.spyk import io.mockk.unmockkStatic import io.mockk.verify +import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runBlockingTest @@ -61,7 +62,7 @@ import org.smartregister.fhircore.engine.app.fakes.Faker import org.smartregister.fhircore.engine.configuration.app.ConfigService import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule -import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.generateMissingId import org.smartregister.fhircore.engine.util.extension.generateMissingVersionId @@ -72,8 +73,6 @@ import org.smartregister.fhircore.engine.util.extension.loadRelatedPersons @HiltAndroidTest class DefaultRepositoryTest : RobolectricTest() { - private val dispatcherProvider = spyk(DefaultDispatcherProvider()) - @get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) @OptIn(ExperimentalCoroutinesApi::class) @@ -81,6 +80,8 @@ class DefaultRepositoryTest : RobolectricTest() { var coroutineRule = CoroutineTestRule() private val configurationRegistry = Faker.buildTestConfigurationRegistry() + @Inject lateinit var dispatcherProvider: DispatcherProvider + @BindValue val sharedPreferencesHelper = mockk(relaxed = true) private val configService: ConfigService = mockk() @@ -126,7 +127,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -167,7 +168,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -189,7 +190,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -209,7 +210,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -232,7 +233,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -258,7 +259,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -280,7 +281,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -301,7 +302,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -326,7 +327,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, @@ -371,7 +372,7 @@ class DefaultRepositoryTest : RobolectricTest() { val defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/AppointmentRegisterDaoTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/AppointmentRegisterDaoTest.kt index f56be992938..9e078c2eeff 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/AppointmentRegisterDaoTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/AppointmentRegisterDaoTest.kt @@ -55,6 +55,7 @@ import org.smartregister.fhircore.engine.domain.model.RegisterData import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.LOGGED_IN_PRACTITIONER import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @@ -72,6 +73,8 @@ class AppointmentRegisterDaoTest : RobolectricTest() { @get:Rule(order = 2) val coroutineTestRule = CoroutineTestRule() + @Inject lateinit var dispatcherProvider: DispatcherProvider + private lateinit var appointmentRegisterDao: AppointmentRegisterDao private val fhirEngine: FhirEngine = mockk() @@ -88,7 +91,7 @@ class AppointmentRegisterDaoTest : RobolectricTest() { defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDaoTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDaoTest.kt index ce8e6e31d67..9deefe09f45 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDaoTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDaoTest.kt @@ -71,6 +71,7 @@ import org.smartregister.fhircore.engine.domain.model.ProfileData import org.smartregister.fhircore.engine.domain.model.RegisterData import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.asReference import org.smartregister.fhircore.engine.util.extension.clinicVisitOrder @@ -90,6 +91,8 @@ internal class HivRegisterDaoTest : RobolectricTest() { @BindValue val sharedPreferencesHelper: SharedPreferencesHelper = mockk(relaxed = true) @Inject lateinit var configService: ConfigService + + @Inject lateinit var dispatcherProvider: DispatcherProvider private lateinit var hivRegisterDao: HivRegisterDao private val fhirEngine: FhirEngine = mockk() @@ -194,7 +197,7 @@ internal class HivRegisterDaoTest : RobolectricTest() { defaultRepository = DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDaoTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDaoTest.kt index a1f5d715609..4abb79240ba 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDaoTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/data/local/register/dao/TracingRegisterDaoTest.kt @@ -65,7 +65,7 @@ import org.smartregister.fhircore.engine.domain.model.ProfileData import org.smartregister.fhircore.engine.domain.model.RegisterData import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule -import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.LOGGED_IN_PRACTITIONER import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.asReference @@ -83,9 +83,10 @@ class TracingRegisterDaoTest : RobolectricTest() { @BindValue val sharedPreferencesHelper = mockk(relaxed = true) + @Inject lateinit var dispatcherProvider: DispatcherProvider + private val fhirEngine = mockk() private val tracingRepository = spyk(TracingRepository(fhirEngine)) - private val dispatcherProvider = DefaultDispatcherProvider() private lateinit var defaultRepository: DefaultRepository private val configurationRegistry = Faker.buildTestConfigurationRegistry() private lateinit var tracingRegisterDao: TracingRegisterDao @@ -101,7 +102,7 @@ class TracingRegisterDaoTest : RobolectricTest() { spyk( DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/rule/CoroutineTestRule.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/rule/CoroutineTestRule.kt index bff0fadce0e..96112ead76b 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/rule/CoroutineTestRule.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/rule/CoroutineTestRule.kt @@ -18,28 +18,14 @@ package org.smartregister.fhircore.engine.rule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestDispatcher -import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain import org.junit.rules.TestWatcher import org.junit.runner.Description -import org.smartregister.fhircore.engine.util.DispatcherProvider +import org.smartregister.fhircore.engine.app.testDispatcher @ExperimentalCoroutinesApi -class CoroutineTestRule(val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()) : - TestWatcher() { - - val testDispatcherProvider = - object : DispatcherProvider { - override fun default() = testDispatcher - - override fun io() = testDispatcher - - override fun main() = testDispatcher - - override fun unconfined() = testDispatcher - } +class CoroutineTestRule : TestWatcher() { override fun starting(description: Description) { Dispatchers.setMain(testDispatcher) diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/sync/SyncBroadcasterTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/sync/SyncBroadcasterTest.kt index 502f73ca939..a8ddacd265f 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/sync/SyncBroadcasterTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/sync/SyncBroadcasterTest.kt @@ -28,6 +28,8 @@ import com.google.android.fhir.OffsetDateTimeTypeAdapter import com.google.android.fhir.sync.SyncJobStatus import com.google.gson.Gson import com.google.gson.GsonBuilder +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.HiltTestApplication import io.mockk.every import io.mockk.just @@ -39,6 +41,7 @@ import io.mockk.unmockkAll import io.mockk.verify import java.time.OffsetDateTime import java.util.UUID +import javax.inject.Inject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableSharedFlow @@ -50,21 +53,23 @@ import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Assert import org.junit.Before +import org.junit.Rule import org.junit.Test import org.smartregister.fhircore.engine.R import org.smartregister.fhircore.engine.app.AppConfigService import org.smartregister.fhircore.engine.app.fakes.Faker import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator import org.smartregister.fhircore.engine.robolectric.RobolectricTest -import org.smartregister.fhircore.engine.rule.CoroutineTestRule import org.smartregister.fhircore.engine.trace.FakePerformanceReporter import org.smartregister.fhircore.engine.trace.PerformanceReporter +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @ExperimentalCoroutinesApi +@HiltAndroidTest class SyncBroadcasterTest : RobolectricTest() { - + @get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) private lateinit var syncBroadcaster: SyncBroadcaster private lateinit var workManager: WorkManager private lateinit var tokenAuthenticator: TokenAuthenticator @@ -72,8 +77,11 @@ class SyncBroadcasterTest : RobolectricTest() { private val sharedPreferencesHelper: SharedPreferencesHelper = mockk() private lateinit var tracer: PerformanceReporter + @Inject lateinit var dispatcherProvider: DispatcherProvider + @Before fun setUp() { + hiltRule.inject() val appContext = ApplicationProvider.getApplicationContext() mockkStatic(WorkManager::class) workManager = mockk() @@ -107,7 +115,7 @@ class SyncBroadcasterTest : RobolectricTest() { configService = mockk(), fhirEngine = mockk(), sharedSyncStatus = sharedSyncStatus, - dispatcherProvider = CoroutineTestRule().testDispatcherProvider, + dispatcherProvider = dispatcherProvider, tracer = tracer, tokenAuthenticator = tokenAuthenticator, sharedPreferencesHelper = sharedPreferencesHelper, @@ -214,7 +222,7 @@ class SyncBroadcasterTest : RobolectricTest() { configService, fhirEngine = mockk(), sharedSyncStatus, - dispatcherProvider = CoroutineTestRule().testDispatcherProvider, + dispatcherProvider = dispatcherProvider, appContext = context, tracer = FakePerformanceReporter(), tokenAuthenticator = tokenAuthenticator, @@ -250,7 +258,7 @@ class SyncBroadcasterTest : RobolectricTest() { configService, fhirEngine = mockk(), sharedSyncStatus, - dispatcherProvider = CoroutineTestRule().testDispatcherProvider, + dispatcherProvider = dispatcherProvider, appContext = context, tracer = FakePerformanceReporter(), tokenAuthenticator = tokenAuthenticatorAlt, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/RegisterViewModelTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/RegisterViewModelTest.kt index b49ba28c8a2..fa7a957a496 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/RegisterViewModelTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/RegisterViewModelTest.kt @@ -41,6 +41,7 @@ import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule import org.smartregister.fhircore.engine.ui.register.RegisterViewModel import org.smartregister.fhircore.engine.ui.register.model.RegisterFilterType +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @HiltAndroidTest @@ -59,6 +60,8 @@ class RegisterViewModelTest : RobolectricTest() { @Inject lateinit var configService: ConfigService + @Inject lateinit var dispatcherProvider: DispatcherProvider + private lateinit var viewModel: RegisterViewModel @Before @@ -74,7 +77,7 @@ class RegisterViewModelTest : RobolectricTest() { fhirResourceDataSource = mockk(), configurationRegistry = configurationRegistry, configService = configService, - dispatcher = coroutineTestRule.testDispatcherProvider, + dispatcher = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, ) } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModelTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModelTest.kt index d0a5f7cc876..04735b4c493 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModelTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModelTest.kt @@ -19,6 +19,8 @@ package org.smartregister.fhircore.engine.ui.appsetting import android.content.Context import androidx.test.core.app.ApplicationProvider import com.google.gson.GsonBuilder +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.HiltTestApplication import io.mockk.coEvery import io.mockk.coVerify @@ -29,6 +31,7 @@ import io.mockk.runs import io.mockk.spyk import io.mockk.verify import java.net.UnknownHostException +import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import okhttp3.MediaType.Companion.toMediaTypeOrNull @@ -38,6 +41,7 @@ import org.hl7.fhir.r4.model.Bundle import org.hl7.fhir.r4.model.Composition import org.hl7.fhir.r4.model.Reference import org.junit.Assert +import org.junit.Before import org.junit.Ignore import org.junit.Rule import org.junit.Test @@ -49,6 +53,7 @@ import org.smartregister.fhircore.engine.data.local.DefaultRepository import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.extension.showToast @@ -56,7 +61,10 @@ import retrofit2.HttpException import retrofit2.Response @OptIn(ExperimentalCoroutinesApi::class) +@HiltAndroidTest class AppSettingViewModelTest : RobolectricTest() { + @get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) + @ExperimentalCoroutinesApi @get:Rule(order = 1) val coroutineTestRule = CoroutineTestRule() private val defaultRepository = mockk() private val fhirResourceDataSource = mockk() @@ -66,21 +74,29 @@ class AppSettingViewModelTest : RobolectricTest() { GsonBuilder().setLenient().create(), ) + @Inject lateinit var dispatcherProvider: DispatcherProvider + private val configService = mockk() - @ExperimentalCoroutinesApi - private val appSettingViewModel = - spyk( - AppSettingViewModel( - fhirResourceDataSource = fhirResourceDataSource, - defaultRepository = defaultRepository, - sharedPreferencesHelper = sharedPreferencesHelper, - configurationRegistry = Faker.buildTestConfigurationRegistry(), - dispatcherProvider = this.coroutineTestRule.testDispatcherProvider, - ), - ) + @ExperimentalCoroutinesApi private lateinit var appSettingViewModel: AppSettingViewModel + private val context = ApplicationProvider.getApplicationContext() + @Before + fun setUp() { + hiltRule.inject() + appSettingViewModel = + spyk( + AppSettingViewModel( + fhirResourceDataSource = fhirResourceDataSource, + defaultRepository = defaultRepository, + sharedPreferencesHelper = sharedPreferencesHelper, + configurationRegistry = Faker.buildTestConfigurationRegistry(), + dispatcherProvider = dispatcherProvider, + ), + ) + } + @Test fun testOnApplicationIdChanged() { appSettingViewModel.onApplicationIdChanged("appId") diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginActivityTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginActivityTest.kt index b77e0d43e17..68978944f15 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginActivityTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginActivityTest.kt @@ -63,6 +63,7 @@ import org.smartregister.fhircore.engine.trace.FakePerformanceReporter import org.smartregister.fhircore.engine.trace.PerformanceReporter import org.smartregister.fhircore.engine.ui.pin.PinSetupActivity import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.FORCE_LOGIN_VIA_USERNAME_FROM_PIN_SETUP import org.smartregister.fhircore.engine.util.SecureSharedPreference import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @@ -105,6 +106,8 @@ class LoginActivityTest : ActivityRobolectricTest() { private val defaultRepository: DefaultRepository = mockk(relaxed = true) private val tokenAuthenticator = mockk() + @Inject lateinit var dispatcherProvider: DispatcherProvider + @OptIn(ExperimentalCoroutinesApi::class) @Before fun setUp() { @@ -126,7 +129,7 @@ class LoginActivityTest : ActivityRobolectricTest() { fhirResourceService = fhirResourceService, tokenAuthenticator = tokenAuthenticator, secureSharedPreference = secureSharedPreference, - dispatcherProvider = coroutineTestRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, fhirResourceDataSource = fhirResourceDataSource, ), ) diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginViewModelTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginViewModelTest.kt index ded7c0eda9c..b3565769fe3 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginViewModelTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/login/LoginViewModelTest.kt @@ -55,6 +55,7 @@ import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator import org.smartregister.fhircore.engine.robolectric.AccountManagerShadow import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SecureSharedPreference import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @@ -81,6 +82,8 @@ internal class LoginViewModelTest : RobolectricTest() { @Inject lateinit var secureSharedPreference: SecureSharedPreference + @Inject lateinit var dispatcherProvider: DispatcherProvider + private lateinit var loginViewModel: LoginViewModel private val fhirResourceService = mockk() private val keycloakService = mockk() @@ -108,7 +111,7 @@ internal class LoginViewModelTest : RobolectricTest() { fhirResourceService = fhirResourceService, tokenAuthenticator = tokenAuthenticator, secureSharedPreference = secureSharedPreference, - dispatcherProvider = coroutineTestRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, fhirResourceDataSource = fhirResourceDataSource, ), ) diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/questionnaire/QuestionnaireViewModelTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/questionnaire/QuestionnaireViewModelTest.kt index a1656cd1bfa..e240779c408 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/questionnaire/QuestionnaireViewModelTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/questionnaire/QuestionnaireViewModelTest.kt @@ -91,6 +91,7 @@ import org.smartregister.fhircore.engine.data.remote.model.response.UserInfo import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule import org.smartregister.fhircore.engine.trace.FakePerformanceReporter +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.LOGGED_IN_PRACTITIONER import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @@ -118,6 +119,8 @@ class QuestionnaireViewModelTest : RobolectricTest() { @Inject lateinit var configService: ConfigService + @Inject lateinit var dispatcherProvider: DispatcherProvider + private val fhirEngine: FhirEngine = mockk() private val context: Application = ApplicationProvider.getApplicationContext() @@ -155,7 +158,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { spyk( DefaultRepository( fhirEngine = fhirEngine, - dispatcherProvider = coroutineRule.testDispatcherProvider, + dispatcherProvider = dispatcherProvider, sharedPreferencesHelper = sharedPreferencesHelper, configurationRegistry = configurationRegistry, configService = configService, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/settings/InfoCardTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/settings/InfoCardTest.kt index 7b18e2e7313..591f260a6de 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/settings/InfoCardTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/settings/InfoCardTest.kt @@ -21,8 +21,8 @@ import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.lifecycle.MutableLiveData -import io.mockk.every -import io.mockk.mockk +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest import kotlinx.coroutines.ExperimentalCoroutinesApi import org.junit.Rule import org.junit.Test @@ -31,29 +31,25 @@ import org.smartregister.fhircore.engine.robolectric.RobolectricTest import org.smartregister.fhircore.engine.rule.CoroutineTestRule @OptIn(ExperimentalCoroutinesApi::class) +@HiltAndroidTest class InfoCardTest : RobolectricTest() { + @get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) + @get:Rule(order = 1) val coroutineRule = CoroutineTestRule() @get:Rule(order = 2) val composeRule = createComposeRule() - private val settingsViewModel = mockk() - @Test fun infoCard_ShowsLoading_WhenStateIsLoading() { - every { settingsViewModel.profileData } returns MutableLiveData(DataLoadState.Loading) - - composeRule.setContent { InfoCard(viewModel = settingsViewModel) } - + composeRule.setContent { InfoCard(profileData = MutableLiveData(DataLoadState.Loading)) } composeRule.onNodeWithTag("ProgressBarItem").assertExists() } @Test fun infoCard_ShowsError_WhenStateIsError() { - every { settingsViewModel.profileData } returns - MutableLiveData(DataLoadState.Error(Exception("Test Error"))) - - composeRule.setContent { InfoCard(viewModel = settingsViewModel) } - + composeRule.setContent { + InfoCard(profileData = MutableLiveData(DataLoadState.Error(Exception("Test Error")))) + } composeRule.onNodeWithText("Something went wrong while fetching data..").assertExists() } @@ -61,10 +57,10 @@ class InfoCardTest : RobolectricTest() { fun infoCard_ShowsContent_WhenStateIsSuccess() { val mockData = ProfileData(userName = "Test User", isUserValid = true, practitionerDetails = null) - every { settingsViewModel.profileData } returns MutableLiveData(DataLoadState.Success(mockData)) - - composeRule.setContent { InfoCard(viewModel = settingsViewModel) } + composeRule.setContent { + InfoCard(profileData = MutableLiveData(DataLoadState.Success(mockData))) + } composeRule.onNodeWithText("Test User", ignoreCase = true).assertExists() } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/userprofile/UserProfileViewModelTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/userprofile/UserProfileViewModelTest.kt index 27cccc628c0..e8cd9901a52 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/userprofile/UserProfileViewModelTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/ui/userprofile/UserProfileViewModelTest.kt @@ -29,6 +29,7 @@ import io.mockk.mockk import io.mockk.runs import io.mockk.spyk import io.mockk.verify +import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.test.runBlockingTest @@ -46,8 +47,8 @@ import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceS import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator import org.smartregister.fhircore.engine.domain.model.Language import org.smartregister.fhircore.engine.robolectric.RobolectricTest -import org.smartregister.fhircore.engine.rule.CoroutineTestRule import org.smartregister.fhircore.engine.sync.SyncBroadcaster +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.SecureSharedPreference import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper @@ -64,6 +65,8 @@ class UserProfileViewModelTest : RobolectricTest() { lateinit var sharedPreferencesHelper: SharedPreferencesHelper @BindValue var configurationRegistry = Faker.buildTestConfigurationRegistry() + + @Inject lateinit var dispatcherProvider: DispatcherProvider var tokenAuthenticator: TokenAuthenticator = mockk() private lateinit var configService: ConfigService @@ -91,7 +94,7 @@ class UserProfileViewModelTest : RobolectricTest() { configService, fhirEngine = mockk(), sharedSyncStatus, - dispatcherProvider = CoroutineTestRule().testDispatcherProvider, + dispatcherProvider = dispatcherProvider, appContext = context, tracer = mockk(), tokenAuthenticator = tokenAuthenticator, diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/RegisterContentTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/RegisterContentTest.kt index 9b94eb3fba0..0e9e7e2e317 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/RegisterContentTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/RegisterContentTest.kt @@ -17,7 +17,7 @@ package org.smartregister.fhircore.quest import com.google.android.fhir.datacapture.mapping.ResourceMapper -import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.runBlocking import org.hl7.fhir.r4.model.Coding import org.hl7.fhir.r4.model.Condition import org.hl7.fhir.r4.model.Encounter @@ -115,7 +115,7 @@ class RegisterContentTest : RobolectricTest() { } @Test - fun testMwCorePopulationResources() = runTest { + fun testMwCorePopulationResources() { val questionnaire = "mwcore-registration/questionnaire.json".readFile().decodeResourceFromString() // Patient().apply { addTags(listOf(Coding("https://www.d-tree.org", "p-category", "P @@ -126,7 +126,7 @@ class RegisterContentTest : RobolectricTest() { codingList.add(Coding("https://www.d-tree.org", "p-category", "hello")) meta.tag.apply { addAll(codingList) } } - val result = ResourceMapper.populate(questionnaire, patient) + val result = runBlocking { ResourceMapper.populate(questionnaire, patient) } Assert.assertEquals( "p-category", result.item.first().itemFirstRep.answerFirstRep.valueCoding.code, @@ -134,7 +134,7 @@ class RegisterContentTest : RobolectricTest() { } @Test - fun testMwCorePopulationResources2() = runTest { + fun testMwCorePopulationResources2() { val structureMap = "mwcore-registration/patient-edit-profile-structure-map.txt".readFile() val response = "mwcore-registration/questionnaire-resposne.json".readFile() diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/app/TestDispatcher.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/TestDispatcher.kt new file mode 100644 index 00000000000..3ba00434f61 --- /dev/null +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/TestDispatcher.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2021 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.quest.app + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher + +@OptIn(ExperimentalCoroutinesApi::class) val testDispatcher = UnconfinedTestDispatcher() diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeDispatcherModule.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeDispatcherModule.kt new file mode 100644 index 00000000000..7620a754fa9 --- /dev/null +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeDispatcherModule.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2021 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.quest.app.fakes + +import dagger.Module +import dagger.Provides +import dagger.hilt.components.SingletonComponent +import dagger.hilt.testing.TestInstallIn +import javax.inject.Singleton +import org.smartregister.fhircore.engine.di.DispatcherModule +import org.smartregister.fhircore.engine.util.DispatcherProvider +import org.smartregister.fhircore.quest.app.testDispatcher + +@Module +@TestInstallIn(components = [SingletonComponent::class], replaces = [DispatcherModule::class]) +class FakeDispatcherModule { + + @Provides + @Singleton + fun provideDispatcherProvider(): DispatcherProvider = + object : DispatcherProvider { + + override fun main() = testDispatcher + + override fun default() = testDispatcher + + override fun io() = testDispatcher + + override fun unconfined() = testDispatcher + } +} diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeFhirEngineModule.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeFhirEngineModule.kt index f3f5c0e0aa8..5ec17cdc8e1 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeFhirEngineModule.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/app/fakes/FakeFhirEngineModule.kt @@ -25,11 +25,9 @@ import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.components.SingletonComponent import dagger.hilt.testing.TestInstallIn import javax.inject.Singleton -import org.smartregister.fhircore.engine.di.DispatcherModule import org.smartregister.fhircore.engine.di.FhirEngineModule -import org.smartregister.fhircore.engine.di.NetworkModule -@Module(includes = [NetworkModule::class, DispatcherModule::class]) +@Module @TestInstallIn(components = [SingletonComponent::class], replaces = [FhirEngineModule::class]) class FakeFhirEngineModule { diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/coroutine/CoroutineTestRule.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/coroutine/CoroutineTestRule.kt index 59e827a1995..441b46845f8 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/coroutine/CoroutineTestRule.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/coroutine/CoroutineTestRule.kt @@ -18,38 +18,20 @@ package org.smartregister.fhircore.quest.coroutine import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestCoroutineDispatcher -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain -import org.junit.rules.TestRule +import org.junit.rules.TestWatcher import org.junit.runner.Description -import org.junit.runners.model.Statement -import org.smartregister.fhircore.engine.util.DispatcherProvider +import org.smartregister.fhircore.quest.app.testDispatcher @ExperimentalCoroutinesApi -class CoroutineTestRule(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : - TestRule, TestCoroutineScope by TestCoroutineScope(testDispatcher) { +class CoroutineTestRule : TestWatcher() { - val testDispatcherProvider = - object : DispatcherProvider { - override fun default() = testDispatcher + override fun starting(description: Description) { + Dispatchers.setMain(testDispatcher) + } - override fun io() = testDispatcher - - override fun main() = testDispatcher - - override fun unconfined() = testDispatcher - } - - override fun apply(base: Statement?, description: Description?) = - object : Statement() { - @Throws(Throwable::class) - override fun evaluate() { - Dispatchers.setMain(testDispatcher) - base?.evaluate() - Dispatchers.resetMain() - testDispatcher.cleanupTestCoroutines() - } - } + override fun finished(description: Description) { + Dispatchers.resetMain() + } } diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/data/PatientRepositoryTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/data/PatientRepositoryTest.kt index 3d564b20bd3..d9d28202dd3 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/data/PatientRepositoryTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/data/PatientRepositoryTest.kt @@ -38,6 +38,7 @@ import java.util.Locale import javax.inject.Inject import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.runTest import org.hl7.fhir.r4.model.CodeableConcept import org.hl7.fhir.r4.model.Coding import org.hl7.fhir.r4.model.Condition @@ -60,6 +61,7 @@ import org.junit.Test import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry import org.smartregister.fhircore.engine.configuration.view.SearchFilter import org.smartregister.fhircore.engine.ui.questionnaire.QuestionnaireConfig +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.engine.util.extension.asDdMmmYyyy import org.smartregister.fhircore.engine.util.extension.decodeJson import org.smartregister.fhircore.quest.app.fakes.Faker.buildPatient @@ -85,6 +87,8 @@ class PatientRepositoryTest : RobolectricTest() { @BindValue var configurationRegistry: ConfigurationRegistry = mockk() + @Inject lateinit var dispatcherProvider: DispatcherProvider + private val fhirEngine: FhirEngine = mockk() private lateinit var repository: PatientRepository @@ -99,7 +103,7 @@ class PatientRepositoryTest : RobolectricTest() { PatientRepository( fhirEngine, patientItemMapper, - coroutineTestRule.testDispatcherProvider, + dispatcherProvider, configurationRegistry, ) } @@ -114,121 +118,116 @@ class PatientRepositoryTest : RobolectricTest() { ) @Test - fun testFetchDemographicsShouldReturnTestPatient() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.get(ResourceType.Patient, "1") } returns - buildPatient("1", "doe", "john", 0) - - val patient = repository.fetchDemographics("1") - Assert.assertEquals("john", patient.name?.first()?.given?.first()?.value) - Assert.assertEquals("doe", patient.name?.first()?.family) - } + fun testFetchDemographicsShouldReturnTestPatient() = runTest { + coEvery { fhirEngine.get(ResourceType.Patient, "1") } returns + buildPatient("1", "doe", "john", 0) + + val patient = repository.fetchDemographics("1") + Assert.assertEquals("john", patient.name?.first()?.given?.first()?.value) + Assert.assertEquals("doe", patient.name?.first()?.family) + } @Test - fun testFetchDemographicsWithAdditionalDataShouldReturnTestPatientWithAdditionalData() = - coroutineTestRule.runBlockingTest { - mockkStatic(::loadAdditionalData) - coEvery { loadAdditionalData(any(), any(), any()) } returns - listOf(AdditionalData("label", "value", "valuePrefix", null)) - coEvery { fhirEngine.get(ResourceType.Patient, "1") } returns - buildPatient("1", "doe", "john", 0) - - val patientItem = repository.fetchDemographicsWithAdditionalData("1") - Assert.assertEquals("John Doe", patientItem.name) - Assert.assertTrue(patientItem.additionalData!!.isNotEmpty()) - unmockkStatic(::loadAdditionalData) - } + fun testFetchDemographicsWithAdditionalDataShouldReturnTestPatientWithAdditionalData() = runTest { + mockkStatic(::loadAdditionalData) + coEvery { loadAdditionalData(any(), any(), any()) } returns + listOf(AdditionalData("label", "value", "valuePrefix", null)) + coEvery { fhirEngine.get(ResourceType.Patient, "1") } returns + buildPatient("1", "doe", "john", 0) + + val patientItem = repository.fetchDemographicsWithAdditionalData("1") + Assert.assertEquals("John Doe", patientItem.name) + Assert.assertTrue(patientItem.additionalData!!.isNotEmpty()) + unmockkStatic(::loadAdditionalData) + } @Test - fun testLoadDataShouldReturnPatientItemList() = - coroutineTestRule.runBlockingTest { - mockkStatic(::loadAdditionalData) - coEvery { loadAdditionalData(any(), any(), any()) } returns listOf() - coEvery { fhirEngine.search(any()) } returns - listOf( - SearchResult( - buildPatient("1234", "Doe", "John", 1, Enumerations.AdministrativeGender.FEMALE), - included = null, - revIncluded = null, - ), - ) - coEvery { fhirEngine.count(any()) } returns 1 + fun testLoadDataShouldReturnPatientItemList() = runTest { + mockkStatic(::loadAdditionalData) + coEvery { loadAdditionalData(any(), any(), any()) } returns listOf() + coEvery { fhirEngine.search(any()) } returns + listOf( + SearchResult( + buildPatient("1234", "Doe", "John", 1, Enumerations.AdministrativeGender.FEMALE), + included = null, + revIncluded = null, + ), + ) + coEvery { fhirEngine.count(any()) } returns 1 - val data = repository.loadData("", 0, true) - Assert.assertEquals("1234", data[0].id) - Assert.assertEquals("John Doe", data[0].name) - Assert.assertEquals("1y", data[0].age) - Assert.assertEquals("F", data[0].gender) - Assert.assertEquals("Female", data[0].genderFull()) + val data = repository.loadData("", 0, true) + Assert.assertEquals("1234", data[0].id) + Assert.assertEquals("John Doe", data[0].name) + Assert.assertEquals("1y", data[0].age) + Assert.assertEquals("F", data[0].gender) + Assert.assertEquals("Female", data[0].genderFull()) - coVerify { fhirEngine.search(any()) } - unmockkStatic(::loadAdditionalData) - } + coVerify { fhirEngine.search(any()) } + unmockkStatic(::loadAdditionalData) + } @Test - fun testCountAllShouldReturnNumberOfPatients() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.count(any()) } returns 1 - val data = repository.countAll() - Assert.assertEquals(1, data) - } + fun testCountAllShouldReturnNumberOfPatients() = runTest { + coEvery { fhirEngine.count(any()) } returns 1 + val data = repository.countAll() + Assert.assertEquals(1, data) + } @Test - fun testFetchTestResultsShouldReturnListOfTestReports() = - coroutineTestRule.runBlockingTest { - val today = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()) - val yesterday = - Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant()) - - coEvery { fhirEngine.get(ResourceType.Questionnaire, "1") } returns - Questionnaire().apply { name = "First Questionnaire" } - - coEvery { fhirEngine.get(ResourceType.Questionnaire, "2") } returns - Questionnaire().apply { name = "Second Questionnaire" } - - coEvery { - fhirEngine.search { - filter(QuestionnaireResponse.SUBJECT, { value = "Patient/1" }) - filter(QuestionnaireResponse.QUESTIONNAIRE, { value = "Questionnaire/1" }) - } - } returns - listOf( - SearchResult( - QuestionnaireResponse().apply { - authored = today - questionnaire = "Questionnaire/1" - }, - included = null, - revIncluded = null, - ), - SearchResult( - QuestionnaireResponse().apply { - authored = yesterday - questionnaire = "Questionnaire/2" - }, - included = null, - revIncluded = null, - ), - ) + fun testFetchTestResultsShouldReturnListOfTestReports() = runTest { + val today = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()) + val yesterday = + Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant()) - val results = - repository.fetchTestResults( - "1", - ResourceType.Patient, - listOf(QuestionnaireConfig("quest", "form", "title", "1")), - dataDetailsListViewConfigurationOf(), - ) + coEvery { fhirEngine.get(ResourceType.Questionnaire, "1") } returns + Questionnaire().apply { name = "First Questionnaire" } - Assert.assertEquals("First Questionnaire", results[0].data[0][0].value) - Assert.assertEquals(" (${today.asDdMmmYyyy()})", results[0].data[0][1].value) + coEvery { fhirEngine.get(ResourceType.Questionnaire, "2") } returns + Questionnaire().apply { name = "Second Questionnaire" } - Assert.assertEquals("Second Questionnaire", results[1].data[0][0].value) - Assert.assertEquals(" (${yesterday.asDdMmmYyyy()})", results[1].data[0][1].value) - } + coEvery { + fhirEngine.search { + filter(QuestionnaireResponse.SUBJECT, { value = "Patient/1" }) + filter(QuestionnaireResponse.QUESTIONNAIRE, { value = "Questionnaire/1" }) + } + } returns + listOf( + SearchResult( + QuestionnaireResponse().apply { + authored = today + questionnaire = "Questionnaire/1" + }, + included = null, + revIncluded = null, + ), + SearchResult( + QuestionnaireResponse().apply { + authored = yesterday + questionnaire = "Questionnaire/2" + }, + included = null, + revIncluded = null, + ), + ) + + val results = + repository.fetchTestResults( + "1", + ResourceType.Patient, + listOf(QuestionnaireConfig("quest", "form", "title", "1")), + dataDetailsListViewConfigurationOf(), + ) + + Assert.assertEquals("First Questionnaire", results[0].data[0][0].value) + Assert.assertEquals(" (${today.asDdMmmYyyy()})", results[0].data[0][1].value) + + Assert.assertEquals("Second Questionnaire", results[1].data[0][0].value) + Assert.assertEquals(" (${yesterday.asDdMmmYyyy()})", results[1].data[0][1].value) + } @Test fun testGetQuestionnaireOfQuestionnaireResponseShouldReturnNonEmptyQuestionnaire() { - coroutineTestRule.runBlockingTest { + runTest { coEvery { fhirEngine.get(ResourceType.Questionnaire, any()) } returns Questionnaire().apply { id = "1" @@ -248,23 +247,22 @@ class PatientRepositoryTest : RobolectricTest() { } @Test - fun testLoadEncounterShouldReturnNonEmptyEncounter() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.get(ResourceType.Encounter, any()) } returns - Encounter().apply { - id = "1" - status = Encounter.EncounterStatus.INPROGRESS - } + fun testLoadEncounterShouldReturnNonEmptyEncounter() = runTest { + coEvery { fhirEngine.get(ResourceType.Encounter, any()) } returns + Encounter().apply { + id = "1" + status = Encounter.EncounterStatus.INPROGRESS + } - val result = repository.loadEncounter("1") + val result = repository.loadEncounter("1") - Assert.assertEquals("1", result.id) - Assert.assertEquals(Encounter.EncounterStatus.INPROGRESS, result.status) - } + Assert.assertEquals("1", result.id) + Assert.assertEquals(Encounter.EncounterStatus.INPROGRESS, result.status) + } @Test fun testGetQuestionnaireOfQuestionnaireResponseShouldReturnEmptyQuestionnaire() { - coroutineTestRule.runBlockingTest { + runTest { coEvery { fhirEngine.get(ResourceType.Questionnaire, any()) } returns Questionnaire() val questionnaire = repository.getQuestionnaire(QuestionnaireResponse()) @@ -276,87 +274,83 @@ class PatientRepositoryTest : RobolectricTest() { } @Test - fun testFetchTestFormShouldReturnListOfQuestionnaireConfig() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.search(any()) } returns - listOf( - SearchResult( - Questionnaire().apply { - name = "g6pd-test" - title = "G6PD Test" - }, - included = null, - revIncluded = null, - ), - ) + fun testFetchTestFormShouldReturnListOfQuestionnaireConfig() = runTest { + coEvery { fhirEngine.search(any()) } returns + listOf( + SearchResult( + Questionnaire().apply { + name = "g6pd-test" + title = "G6PD Test" + }, + included = null, + revIncluded = null, + ), + ) - val results = repository.fetchTestForms(searchFilter()) + val results = repository.fetchTestForms(searchFilter()) - with(results.first()) { - Assert.assertEquals("g6pd-test", form) - Assert.assertEquals("G6PD Test", title) - } + with(results.first()) { + Assert.assertEquals("g6pd-test", form) + Assert.assertEquals("G6PD Test", title) } + } @Test - fun testFetchTestFormShouldHandleNullNameAndTitle() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.search(any()) } returns - listOf( - SearchResult(Questionnaire().apply { id = "1234" }, included = null, revIncluded = null), - ) + fun testFetchTestFormShouldHandleNullNameAndTitle() = runTest { + coEvery { fhirEngine.search(any()) } returns + listOf( + SearchResult(Questionnaire().apply { id = "1234" }, included = null, revIncluded = null), + ) - val results = repository.fetchTestForms(searchFilter()) + val results = repository.fetchTestForms(searchFilter()) - with(results.first()) { - Assert.assertEquals("1234", form) - Assert.assertEquals("1234", title) - } + with(results.first()) { + Assert.assertEquals("1234", form) + Assert.assertEquals("1234", title) } + } @Test - fun testFetchTestFormShouldHandleNullTitle() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.search(any()) } returns - listOf( - SearchResult( - Questionnaire().apply { - id = "1234" - name = "Form name" - }, - included = null, - revIncluded = null, - ), - ) + fun testFetchTestFormShouldHandleNullTitle() = runTest { + coEvery { fhirEngine.search(any()) } returns + listOf( + SearchResult( + Questionnaire().apply { + id = "1234" + name = "Form name" + }, + included = null, + revIncluded = null, + ), + ) - val results = repository.fetchTestForms(searchFilter()) - with(results.first()) { - Assert.assertEquals("Form name", form) - Assert.assertEquals("Form name", title) - } + val results = repository.fetchTestForms(searchFilter()) + with(results.first()) { + Assert.assertEquals("Form name", form) + Assert.assertEquals("Form name", title) } + } @Test - fun testFetchTestFormShouldHandleNullName() = - coroutineTestRule.runBlockingTest { - coEvery { fhirEngine.search(any()) } returns - listOf( - SearchResult( - Questionnaire().apply { - id = "1234" - title = "Form name" - }, - included = null, - revIncluded = null, - ), - ) + fun testFetchTestFormShouldHandleNullName() = runTest { + coEvery { fhirEngine.search(any()) } returns + listOf( + SearchResult( + Questionnaire().apply { + id = "1234" + title = "Form name" + }, + included = null, + revIncluded = null, + ), + ) - val results = repository.fetchTestForms(searchFilter()) - with(results.first()) { - Assert.assertEquals("1234", form) - Assert.assertEquals("Form name", title) - } + val results = repository.fetchTestForms(searchFilter()) + with(results.first()) { + Assert.assertEquals("1234", form) + Assert.assertEquals("Form name", title) } + } fun createTestConfigurationsData(): List = "configs/sample_patient_details_view_configurations.json".readFile().decodeJson() diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/data/patient/HivPatientGuardianRepositoryTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/data/patient/HivPatientGuardianRepositoryTest.kt index 3e63c9f2904..a299177d626 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/data/patient/HivPatientGuardianRepositoryTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/data/patient/HivPatientGuardianRepositoryTest.kt @@ -16,9 +16,12 @@ package org.smartregister.fhircore.quest.data.patient +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk +import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.hl7.fhir.r4.model.Patient @@ -27,19 +30,25 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.smartregister.fhircore.engine.domain.repository.PatientDao +import org.smartregister.fhircore.engine.util.DispatcherProvider import org.smartregister.fhircore.quest.coroutine.CoroutineTestRule @OptIn(ExperimentalCoroutinesApi::class) +@HiltAndroidTest class HivPatientGuardianRepositoryTest { - @get:Rule val coroutineTestRule = CoroutineTestRule() + @get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) + + @get:Rule(order = 1) val coroutineTestRule = CoroutineTestRule() + + @Inject lateinit var dispatcherProvider: DispatcherProvider private val patientDao: PatientDao = mockk() private lateinit var hivPatientGuardianRepository: HivPatientGuardianRepository @Before fun setUp() { - hivPatientGuardianRepository = - HivPatientGuardianRepository(patientDao, coroutineTestRule.testDispatcherProvider) + hiltRule.inject() + hivPatientGuardianRepository = HivPatientGuardianRepository(patientDao, dispatcherProvider) } @Test