Skip to content

Commit ed02413

Browse files
committed
Use test dispatcher & real DatabaseService in tests
Add mockk android test dependency and refactor RealmUserTest to use a real DatabaseService constructed with a test DispatcherProvider (all dispatchers set to Dispatchers.Unconfined). Removed the previous manual anonymous DatabaseService implementation and adjusted imports (added CoroutineDispatcher). This simplifies coroutine testing and ensures the database service uses a consistent test dispatcher and application context.
1 parent 24dc15f commit ed02413

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ dependencies {
178178
androidTestImplementation(libs.test.rxjava)
179179
androidTestImplementation(libs.test.arch)
180180
androidTestImplementation(libs.test.hilt)
181+
androidTestImplementation(libs.test.mockkdroid)
181182
kspAndroidTest(libs.hilt.android.compiler)
182183
}
183184
dependencies {

app/src/androidTest/java/org/ole/planet/myplanet/model/RealmUserTest.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dagger.Lazy
88
import io.mockk.mockk
99
import io.realm.Realm
1010
import io.realm.RealmConfiguration
11+
import kotlinx.coroutines.CoroutineDispatcher
1112
import kotlinx.coroutines.CoroutineScope
1213
import kotlinx.coroutines.Dispatchers
1314
import kotlinx.coroutines.runBlocking
@@ -45,22 +46,13 @@ class RealmUserTest {
4546
Realm.setDefaultConfiguration(realmConfiguration)
4647
}
4748

48-
databaseService = object : DatabaseService {
49-
override val realm: Realm
50-
get() = Realm.getInstance(realmConfiguration)
51-
52-
override fun executeTransaction(transaction: Realm.Transaction) {
53-
realm.executeTransaction(transaction)
54-
}
55-
56-
override fun executeTransactionAsync(
57-
transaction: Realm.Transaction,
58-
onSuccess: Realm.Transaction.OnSuccess,
59-
onError: Realm.Transaction.OnError
60-
) {
61-
realm.executeTransactionAsync(transaction, onSuccess, onError)
62-
}
49+
val testDispatcherProvider = object : DispatcherProvider {
50+
override val main: CoroutineDispatcher = Dispatchers.Unconfined
51+
override val io: CoroutineDispatcher = Dispatchers.Unconfined
52+
override val default: CoroutineDispatcher = Dispatchers.Unconfined
53+
override val unconfined: CoroutineDispatcher = Dispatchers.Unconfined
6354
}
55+
databaseService = DatabaseService(ApplicationProvider.getApplicationContext(), testDispatcherProvider)
6456

6557
val mockSettings = mockk<SharedPreferences>(relaxed = true)
6658
val mockSharedPrefManager = mockk<SharedPrefManager>(relaxed = true)

0 commit comments

Comments
 (0)