Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

后端 未结 7 942
攒了一身酷
攒了一身酷 2021-01-03 20:33

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 wh

相关标签:
7条回答
  • 2021-01-03 20:50

    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"
    
    0 讨论(0)
  • 2021-01-03 20:50

    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.

    0 讨论(0)
  • 2021-01-03 20:53

    I got the same error because I forgot to add androidx.test:runner test dependency.

    androidTestImplementation "androidx.test:runner:1.2.0"
    
    0 讨论(0)
  • 2021-01-03 20:56

    FYI I ran into this and had to add

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

    within my <application/> tag

    0 讨论(0)
  • 2021-01-03 20:57

    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.

    0 讨论(0)
  • 2021-01-03 21:00

    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.

    0 讨论(0)
提交回复
热议问题