Test run failed: Instrumentation run failed due to

2019-02-12 01:19发布

问题:

This is my first time setting up an Android test project to test a Android project.

I've created a very basic test case which I'm trying to get to run, however what I have does not run. I get a Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'.

I would troubleshot this more, however I don't get any more information, such as which class it is trying to find, etc. Any ideas on how to get more information from the error? Any common areas I should look at, or anything that might need to be configured that I'm overlooking?

Below is the information from the console:

[2013-04-16 13:21:49 - XyzProgramTest] Android Launch!
[2013-04-16 13:21:49 - XyzProgramTest] adb is running normally.
[2013-04-16 13:21:49 - XyzProgramTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2013-04-16 13:21:49 - XyzProgramTest] Automatic Target Mode: launching new emulator with compatible AVD 'GalaxyNexusAPI_17'
[2013-04-16 13:21:49 - XyzProgramTest] Launching a new emulator with Virtual Device 'GalaxyNexusAPI_17'
[2013-04-16 13:21:53 - Emulator] extension WGL_ARB_make_current_read was not found
[2013-04-16 13:21:53 - Emulator] extension WGL_EXT_swap_control was not found
[2013-04-16 13:21:53 - Emulator] Failed to create pbuf surface for FB 0x3004
[2013-04-16 13:21:53 - Emulator] emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
[2013-04-16 13:21:54 - XyzProgramTest] New emulator found: emulator-5554
[2013-04-16 13:21:54 - XyzProgramTest] Waiting for HOME ('android.process.acore') to be launched...
[2013-04-16 13:22:55 - XyzProgramTest] HOME is up on device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Uploading XyzProgramTest.apk onto device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Installing XyzProgramTest.apk...
[2013-04-16 13:23:57 - XyzProgramTest] Success!
[2013-04-16 13:23:57 - XyzProgramTest] Project dependency found, installing: XyzProgram
[2013-04-16 13:23:57 - XyzProgram] Uploading XyzProgram.apk onto device 'emulator-5554'
[2013-04-16 13:23:58 - XyzProgram] Installing XyzProgram.apk...
[2013-04-16 13:24:05 - XyzProgram] Success!
[2013-04-16 13:24:05 - XyzProgramTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2013-04-16 13:24:07 - XyzProgramTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

Additional Notes

In my Android test project, I created a simple test with a package similar to the package that matches up with the class I'm trying to test in my Android project. So something like com.company.android.projectname. I've specified this in the Android test project manifest file's instrumentation section.

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.company.android.projectname" />

When I changed this to match that, I get a new error that says Test run failed: Unable to find instrumentation target package: com.company.android.projectname

I'm not 100% sure what that targetPackage should be and if I need multiple instrumentations for each package I want to test, etc. When I set it to com.company.android that is when I get the java.lang.ClassNotFoundException.

回答1:

FYI I ran into this and had to add

<uses-library android:name="android.test.runner"/>

within my <application/> tag



回答2:

Figured out my issue and am posting the answer for documentation purposes...

The root of my problem was 2 different things:

  1. I did some refactoring, which change the location of my android.app.Application class and my activities. This made it so my main program was not working as I needed to update my manifest in my android project.
  2. After making the updates to my manifest in my android project, I had to make some updates in my android test project to point to the updated location of the class that extends android.app.Application.

After that, did some cleans and tested again and things were good.



回答3:

I had upgraded to androidx libraries and started getting this error.

To fix it, in build.gradle I changed the line:

testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

to

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"


回答4:

In my case, I was trying to run the tests on Jelly Bean emulator. Android instrumentation does not seem to support Jelly Bean. I could run the tests on lollipop.

You may also not able to run android tests through release build type. Ensure that debug build type is active.



回答5:

I had this problem after a merge, in which build.gradle was specifying the wrong test package, since I had been using the default.

By simply removing these lines from build.gradle, it was able to find the necessary classes in the default package, which is src/androidTest.

sourceSets {
    instrumentTest.setRoot('src/instrumentTest')
}
//removed this to use the default androidTest source set.

This will probably not be the exact solution, but hopefully guides someone in the right direction.



回答6:

I've encountered this problem today, I adopted android-specific ant build.xml which sitting in "${sdk.dir}/tools/ant/" directory to perform all the test cases in command-line, but the STDOUT reports error to me just like yours:

vinceMacBook ~/dev/aSQLite+-android/tests$ ant clean uninstall debug install test
...
test:
     [echo] Running tests ...
     [exec] INSTRUMENTATION_RESULT: shortMsg=java.lang.ClassNotFoundException
     [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.ClassNotFoundException:
        android.test.InstrumentationTestRunner
     [exec] INSTRUMENTATION_CODE: 0

I spend a few time to figuring out this, I found the main cause due in the root module's AndroidManifest.xml, I thought the application element is nothing to be configure so I omitted it in the manifest element, which causing my problem.

I don't sure you share the same structure with me, so I post my project structure here, also the key file's content, hope can help somebody.

vinceMacBook ~/dev/aSQLite+-android$ tree
.
├── AndroidManifest.xml
│       <?xml version="1.0" encoding="utf-8"?>
│       <manifest
│           xmlns:android="http://schemas.android.com/apk/res/android"
│           package="com.vincestyling.asqliteplus"
│           android:versionCode="1"
│           android:versionName="0.1">
│
│           <uses-sdk android:minSdkVersion="8" />
│           
│           <!-- important configuration, cannot be omit even if nothing to say. -->
│           <application />
│
│       </manifest>
│
├── build.xml
│       unchanged, generated by "android create project <project_name>" command.
│
├── project.properties
│       unchanged, generated by "android create project <project_name>" command.
│
├── src
│   └── com
│       └── vincestyling
│           └── asqliteplus
│               ├── DBOperator.java
│               ├── DBOverseer.java
│               └── the library source code lie here...
│
└── tests
    ├── AndroidManifest.xml
    │       <?xml version="1.0" encoding="utf-8"?>
    │       <manifest
    │           xmlns:android="http://schemas.android.com/apk/res/android"
    │           package="com.vincestyling.asqliteplus.tests">
    │
    │           <application>
    │               <uses-library android:name="android.test.runner"/>
    │           </application>
    │
    │           <instrumentation
    │               android:name="android.test.InstrumentationTestRunner"
    │               android:targetPackage="com.vincestyling.asqliteplus"/>
    │
    │       </manifest>
    │
    ├── ant.properties
    │       # [Comments], [Comments], [Comments] which generated by command.
    │       # [Comments], [Comments], [Comments] which generated by command.
    │       # [Comments], [Comments], [Comments] which generated by command.
    │
    │       # important configuration, point to the root library module.
    │       tested.project.dir=../
    │
    ├── build.xml
    │       unchanged, generated by "android create test-project <project_name>" command.
    │
    └── src
        └── com
            └── vincestyling
                └── asqliteplus
                    ├── BaseDBTest.java
                    ├── QueryStatementTest.java
                    └── the test source code lie here...

My project structure followed Android Test Projects's description which Google suggest us to do, consist of root library module which carrying core logic, and test module which inside the root module. Above are the key resources in project, but weren't all files, I shall post the project link here once I release it.

------------ Update 2014-01-06 --------------

Currently, my project was published on my github : aSQLitePlus-android, check the details if interested.