1

My test using robolectric is failing when I run "gradle test" from the command line, but it passed in Android studio.

But eventually I need to configure my project in CI so need to tun build and test from 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)
GhostCat
  • 137,827
  • 25
  • 176
  • 248

1 Answers1

0

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!

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.

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248