After working with datastore for some time and writing some business logic with it, I would like to test my classes / repository that use datastore. The problem I encounter is, how should I do this? I tried to create a context.createDatastore(...)
with the test context, but I just got some errors. There are no official documentations about this or at least I couldn't find one.
Here is my current approach:
Interface
interface ShopFilterDataStoreRepository {
val shopFilterProduct: Flow<ShopFilterProduct>
val shopFilterList: Flow<ShopFilterList>
suspend fun setValueProduct(newProduct: ShopFilterTempHolder)
suspend fun setValueList(newList: ShopFilterTempHolder)
}
Implementation (Default Implementation used in production)
class ShopFilterDataStoreRepositoryImpl @Inject constructor(
@ApplicationContext private val context: Context,
) : ShopFilterDataStoreRepository {
private companion object {
private const val SHOP_FILTER_PRODUCT_DATASTORE: String = "shop_filter_product_datastore"
private const val SHOP_FILTER_LIST_DATASTORE: String = "shop_filter_list_datastore"
}
// this causes error because test context != applicationcontext
private val nonVolatileProductDataStore = context.createDataStore(
fileName = SHOP_FILTER_PRODUCT_DATASTORE,
serializer = ShopFilterProductSerializer
)
// this causes error because test context != applicationcontext
private val nonVolatileListDataStore = context.createDataStore(
fileName = SHOP_FILTER_LIST_DATASTORE,
serializer = ShopFilterListSerializer
)
override val shopFilterProduct: Flow<ShopFilterProduct>
get() = nonVolatileProductDataStore.data
override val shopFilterList: Flow<ShopFilterList>
get() = nonVolatileListDataStore.data
override suspend fun setValueProduct(newProduct: ShopFilterTempHolder) {
if (newProduct.id == null || newProduct.mQuery == null) return
nonVolatileProductDataStore.updateData { preferences ->
preferences.toBuilder().apply {
positionId = newProduct.id
query = newProduct.mQuery
}.build()
}
}
override suspend fun setValueList(newList: ShopFilterTempHolder) {
if (newList.id == null || newList.mQuery == null) return
nonVolatileListDataStore.updateData { preferences ->
preferences.toBuilder().apply {
positionId = newList.id
query = newList.mQuery
mQueryDirection = newList.mQueryDirection
}.build()
}
}
}
My Test
@RunWith(RobolectricTestRunner::class)
@Config(maxSdk = Build.VERSION_CODES.P, minSdk = Build.VERSION_CODES.P)
class ShopFilterValidatorTest {
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private lateinit var shopFilterValidator: ShopFilterValidator
private lateinit var shopFilterDataStoreRepository: ShopFilterDataStoreRepository
@Before
fun setup() {
shopFilterDataStoreRepository = ShopFilterDataStoreRepositoryImpl(context)
shopFilterValidator = ShopFilterValidator(shopFilterDataStoreRepository)
}
@Test
fun `test should start`() {
}
}
Error Stacktrace
java.lang.ExceptionInInitializerError
at androidx.datastore.core.DataStoreFactory.create(DataStoreFactory.kt:66)
at androidx.datastore.DataStoreFactoryKt.createDataStore(DataStoreFactory.kt:58)
at androidx.datastore.DataStoreFactoryKt.createDataStore$default(DataStoreFactory.kt:56)
at com.example.app.framework.datasource.repository.shop.filter.ShopFilterDataStoreRepositoryImpl.<init>(ShopFilterDataStoreRepositoryImpl.kt:28)
at com.example.app.validator.ShopFilterValidatorTest.setup(ShopFilterValidatorTest.kt:30)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
at androidx.datastore.core.DataMigrationInitializer.<clinit>(DataMigrationInitializer.kt)
at androidx.datastore.core.DataStoreFactory.$$robo$$androidx_datastore_core_DataStoreFactory$create(DataStoreFactory.kt:66)
at androidx.datastore.core.DataStoreFactory.create(DataStoreFactory.kt)
at androidx.datastore.DataStoreFactoryKt.$$robo$$androidx_datastore_DataStoreFactoryKt$createDataStore(DataStoreFactory.kt:58)
at androidx.datastore.DataStoreFactoryKt.createDataStore(DataStoreFactory.kt)
at androidx.datastore.DataStoreFactoryKt.createDataStore$default(DataStoreFactory.kt:56)
at com.example.app.framework.datasource.repository.shop.filter.ShopFilterDataStoreRepositoryImpl.<init>(ShopFilterDataStoreRepositoryImpl.kt:28)
at com.example.app.validator.ShopFilterValidatorTest.setup(ShopFilterValidatorTest.kt:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
... 14 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
... 27 more
Caused by: java.lang.BootstrapMethodError: java.lang.IllegalAccessError: tried to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.datastore.core.DataMigrationInitializer$Companion
at androidx.datastore.core.DataMigrationInitializer$Companion.<init>(DataMigrationInitializer.kt)
at androidx.datastore.core.DataMigrationInitializer.__staticInitializer__(DataMigrationInitializer.kt)
... 33 more
Caused by: java.lang.IllegalAccessError: tried to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.datastore.core.DataMigrationInitializer$Companion
... 35 more
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…