Faux Pas Faux Pas - 1 year ago 63
Android Question

Android test cases failing after upgrading to Java 8 and build tools version 25

I had a bunch of test cases which were running fine. But once my project was updated to build tools version 25 and Java was updated to JDK 1.8, all my test cases are failing.

When I run my tests I using ./gradlew cAT, I get the error message:

Instrumentation run failed due to 'java.lang.NullPointerException'
com.android.builder.testing.ConnectedDevice > No tests found.[Nexus 6 - 6.0.1] FAILED


Tests on Nexus 6 - 6.0.1 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

com.android.builder.testing.ConnectedDevice > No tests found.[Nexus 6 - 6.0.1] FAILED

My build.gradle has the following lines:

androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

Any idea what the solution might be?


When I try to run the same from gradle using ./gradlew -cAT -s, I see the following exception:

Caused by: java.lang.NoClassDefFoundError: org/gradle/logging/ConsoleRenderer
at com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask.runTests(DeviceProviderInstrumentTestTask.java:140)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:228)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:221)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:210)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:621)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:604)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 68 more

Googling tells me that this might be a gradle 2.14 specific problem, but my tests were working fine previously at the same gradle version.

Answer Source

Turns out, the root cause was a null pointer during the initialization of my base context!

My learnings:

  • Irrespective of whether we use the IDE or gradle to run the test cases, they finally make a call to the am instrumentation command (https://developer.android.com/studio/test/command-line.html).

  • Run the command adb shell am instrument -w -r -e debug true -e class (Note that I have set debug to true).

  • Simultaneously, have your adb logcat running. This told me why exactly I encountered the NPE.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download