简体   繁体   English

robolectric测试在cli中失败,并带有java.lang.RuntimeException:java.lang.NoClassDefFoundError:android / content / pm / PackageManager $ NameNotFoundException

[英]robolectric test fails in cli with java.lang.RuntimeException: java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException

My test using robolectric is failing when I run "gradle test" from the command line, but it passed in Android studio. 当我从命令行运行“渐变测试”时,我使用robolectric的测试失败,但该测试在Android Studio中通过。

But eventually I need to configure my project in CI so need to tun build and test from cli. 但是最终我需要在CI中配置我的项目,因此需要对cli进行构建和测试。

So, how could I go about this? 那么,我该怎么做呢?

This here is the exception stack trace: 这是异常堆栈跟踪:

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)

The key issue is here: 关键问题在这里:

Caused by: java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException

This simply tells us: there is a difference in your setup between android studio, and the information that you put into your gradle build setup! 这只是告诉我们:android studio的设置与放入gradle构建设置的信息有所不同!

In Java, you need to correctly specify your dependencies so that A) the compiler can find them for compiling and then B), at runtime, the JVM can load the classes you are using. 在Java中,您需要正确指定依赖项,以便A)编译器可以找到它们进行编译,然后B)在运行时,JVM可以加载您正在使用的类。

You can start reading here for the underlying issue, beyond that, for your specific issue this might be helpful. 您可以从这里开始阅读潜在问题,除此之外,对于您的特定问题, 可能会有所帮助。

In the end, it must be about subtle differences in the setup of your android studio versus gradle configurations, in the area of dependencies. 最后,在依赖性方面,它必须与android studio的设置和gradle配置的细微差别有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android + Robolectric测试错误“ java.lang.RuntimeException:存根!” - Android+Robolectric Test ERROR “java.lang.RuntimeException: Stub!” Android工具测试:java.lang.RuntimeException:java.lang.NoClassDefFoundError:com.google.common.base.Optional - Android instrumentation test: java.lang.RuntimeException: java.lang.NoClassDefFoundError: com.google.common.base.Optional Robolectric:java.lang.RuntimeException:java.lang.ClassNotFoundException:org.robolectric.android.internal.ParallelUniverse - Robolectric: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.robolectric.android.internal.ParallelUniverse Robolectric PowerMock运行测试时出现java.lang.NoClassDefFoundError错误 - Robolectric PowerMock java.lang.NoClassDefFoundError error while running a test 包含Robolectric时在Espresso Test上的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError on Espresso Test when including Robolectric android-java.lang.RuntimeException - android - java.lang.RuntimeException java.lang.RuntimeException android - java.lang.RuntimeException android Android java.lang.RuntimeException - Android java.lang.RuntimeException java.lang.NoClassDefFoundError:android 和 junit 测试 - java.lang.NoClassDefFoundError:android and junit test java.lang.NoClassDefFoundError:android / test / AndroidTestCase - java.lang.NoClassDefFoundError: android/test/AndroidTestCase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM