我的应用程序中有一个实用程序类,该实用程序类将服务器中的用户数据保存到SharedPreferences上。我正在尝试测试名为UserLocalStore.kt
我编写了以下测试类来做到这一点:
@RunWith(AndroidJUnit4::class)
class UserLocalStoreTest {
lateinit var userLocalStore: UserLocalStore
lateinit var context: Context
lateinit var user: User
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().context
userLocalStore = UserLocalStore(context)
userLocalStore.clearUserData()
user = User(
1,
"test",
"test@test.com",
"test_url",
"9817******",
"test address",
1,
"yes",
"2019-02-02",
"2019-03-03",
1
)
}
@Test
fun storeUserData() {
//get an instance of the editor
val userLocalDatabaseEditor = context.getSharedPreferences(
Constants.USER_LOCAL_STORE_SHARED_PREF_NAME,
Context.MODE_PRIVATE
).edit()
//use the editor to put the User model into the shared preference
userLocalDatabaseEditor.putString(UserLocalStore.SERIALIZED_DATA_KEY, user.serialize())
//assert that the data has indeed been stored
val committed = userLocalDatabaseEditor.commit()
assertTrue(committed)
}
运行测试方法时,我得到一个ClassNotFoundException
。我在这里做错了什么?
完整的错误消息为:
java.lang.NoClassDefFoundError: androidx/test/platform/app/InstrumentationRegistry
at com.buyraksi.deliveryapp.util.UserLocalStoreTest.setUp(UserLocalStoreTest.kt:21)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: androidx.test.platform.app.InstrumentationRegistry
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
我认为我的依赖项设置正确,因为我在项目应用级别build.gradle
中具有以下测试依赖项:
espresso_version = '3.2.0'
/*
Testing Dependencies
*/
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version"