当我从命令行运行“渐变测试”时,使用robolectric的测试失败,但该测试在Android Studio中通过。
但是最终我需要在CI中配置我的项目,因此需要对cli进行构建和测试。
那么,我该怎么办?
这是异常堆栈跟踪:
java.lang.RuntimeException: java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException
at hkhc.electricspock.internal.ElectricSpockInterceptor.interceptSpecExecution(ElectricSpockInterceptor.java:59)
at org.spockframework.runtime.extension.AbstractMethodInterceptor.intercept(AbstractMethodInterceptor.java:52)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at hkhc.electricspock.ElectricSputnik.run(ElectricSputnik.java:214)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException
at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3090)
at java.base/java.lang.Class.getConstructor0(Class.java:3295)
at java.base/java.lang.Class.getConstructor(Class.java:2110)
at org.robolectric.RobolectricTestRunner.getHooksInterface(RobolectricTestRunner.java:507)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:323)
at hkhc.electricspock.internal.ContainedRobolectricTestRunner.containedBeforeTest(ContainedRobolectricTestRunner.java:124)
at hkhc.electricspock.internal.ElectricSpockInterceptor.interceptSpecExecution(ElectricSpockInterceptor.java:56)
... 28 more
Caused by: java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:132)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:107)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:106)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 35 more
Caused by: java.lang.RuntimeException: failed to instrument android.content.pm.PackageManager$NameNotFoundException
at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:125)
at org.robolectric.internal.bytecode.InvokeDynamicClassInstrumentor.instrument(InvokeDynamicClassInstrumentor.java:18)
at org.robolectric.internal.bytecode.ClassInstrumentor.instrumentToBytes(ClassInstrumentor.java:67)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$maybeInstrumentClass$2(SandboxClassLoader.java:124)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:123)
... 40 more
Caused by: java.lang.IllegalArgumentException
at org.robolectric.internal.bytecode.ClassNodeProvider.createClassNode(ClassNodeProvider.java:24)
at org.robolectric.internal.bytecode.ClassNodeProvider.getClassNode(ClassNodeProvider.java:16)
at org.robolectric.internal.bytecode.ClassInstrumentor.isOverridingFinalMethod(ClassInstrumentor.java:218)
at org.robolectric.internal.bytecode.ClassInstrumentor.createInstrumentableMethodIfNotAlreadyPresent(ClassInstrumentor.java:236)
at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:105)
答案 0 :(得分:0)
关键问题在这里:
Caused by: java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException
这只是告诉我们:android studio中的设置与输入到gradle构建设置中的信息有所不同!
在Java中,您需要正确指定依赖项,以便A)编译器可以找到它们进行编译,然后B)在运行时,JVM可以 load 您正在使用的类。
您可以开始阅读here的基本问题,除此之外,对于您的特定问题this可能会有所帮助。
最后,在依赖性方面,它必须与android studio的设置与gradle配置的细微差别有关。