gpt4 book ai didi

android - 如何测试 androidx (proto) 数据存储,java.lang.ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-12-04 08:16:23 25 4
gpt4 key购买 nike

在使用数据存储一段时间并用它编写一些业务逻辑之后,我想测试我的使用数据存储的类/存储库。我遇到的问题是,我该怎么做?我试图创建一个 context.createDatastore(...)使用测试上下文,但我遇到了一些错误。没有关于此的官方文档,或者至少我找不到。
这是我目前的方法:
界面

interface ShopFilterDataStoreRepository {
val shopFilterProduct: Flow<ShopFilterProduct>
val shopFilterList: Flow<ShopFilterList>

suspend fun setValueProduct(newProduct: ShopFilterTempHolder)
suspend fun setValueList(newList: ShopFilterTempHolder)
}
实现(生产中使用的默认实现)
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()
}
}
}
我的测试
@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`() {

}
}
错误堆栈跟踪
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

最佳答案

两种选择:

  • 要么升级到 Robolectric 4.5(这是你所缺少的:https://github.com/robolectric/robolectric/commit/e529de42f38cd2372eeb459b1a7c53139d2c754b)
  • 或者,覆盖 RobolectricTestRunner并手动添加androidx.datastore将包裹发送至不是 乐器 :
    class DataStoreRobolectricTestRunner : RobolectricTestRunner {

    constructor(testClass: Class<*>) : super(testClass) {}

    constructor(testClass: Class<*>, injector: Injector) : super(testClass, injector) {}

    override fun createClassLoaderConfig(method: FrameworkMethod?): InstrumentationConfiguration {
    val parentClassLoaderConfig = super.createClassLoaderConfig(method)
    val builder = InstrumentationConfiguration.Builder(parentClassLoaderConfig)

    builder.doNotInstrumentPackage("androidx.datastore")

    return builder.build()
    }
    }
  • 关于android - 如何测试 androidx (proto) 数据存储,java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65685099/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com