4

Every time I start my Android instrumentation test it fails with

android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:
Error in testSuiteConstructionFailed:
java.lang.RuntimeException: Exception during suite construction
    at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
    at com.samy4me.test.Test_Service2.<init>(Test_Service2.java:26)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
    at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
    at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
    at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
    at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
    at android.app.ActivityThread.access$3000(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.XXX.ws.Service
    ... 19 more

The class is there. I checked ProGuard, I temporarily deactivated ProGuard. I reduced the test to the absolute minimum:

public class Test_Service2
   extends android.test.ServiceTestCase<Service>
{

   public Test_Service2 ()
   {
      super (Service.class);
   } // Test_Service2
} // Test_Service2

Has anybody got an idea how this might have come about?

Looking closer I now found the following:

04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.DexFile.defineClass(Native Method)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:209)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-23 15:56:21.186 W/ClassPathPackageInfoSource(  580):     ... 26 more

This is part of the original error exception chain but was truncated. The pre-verified exception is not new and yet another pain in Android development.

Martin
  • 11,577
  • 16
  • 80
  • 110

2 Answers2

10

OK, I found the problem - mostly PEBKAC. So here it is:

  1. I have looked only at the console. But there the error message was truncated. Lesson learned: always check logcat as well.
  2. The real error was the well know pre-verified error which happens when libraries meant for the main application get dragged into test application. Lesson learned: always make sure that libraries are not dragged into the instrumentation test

If you use Maven — like I do — then you can read the details up here Automate Android Test Project, which — for added embarrassment — I wrote myself two weeks ago.

Martin
  • 11,577
  • 16
  • 80
  • 110
  • BTW - in Eclipse ADT, this means that you need to right click on the test project, select `Properties` -> `Java Build Path`, remove the libraries included by your target project from `Projects`. Also, click on `Android`, and in the `Library` box, remove any projects that are included by your target project. – jwir3 Feb 14 '14 at 18:45
0

The test is correct and should work. Be sure that Service and Service.class refer to com.XXX.ws.Service and not android.app.Service.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I tried fully qualified named just to be sure and it still did not work. I tried renaming Service for Service2 as well just to be sure the then the error message tells me that Service2 is missing. This is pretty annoying because as you say: It should work. A problem in the AndroidManifest.xml perhaps. – Martin Apr 23 '11 at 11:30