Android Studio Unit Testing: unable to find instrumentation OR class not found ex
Asked Answered
A

6

17

I have a test suite within my Android studio that has the following dir structure:

-MainProject
 -src
  --com
  --tests
    --java.xx.xxx.xx.test

Within my AndroidManifest, I have the following:

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="tests.java.xxx.xxx.xxx.test" />

When I run my tests from the command line using: ./gradlew connectedInstrumentTest, I simply get:

Tests on Nexus 5 - 4.4.2 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

When I run the tests from inside Android Studio I get:

Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{tests.xxx.xxx.xxx.xxx.test/android.test.InstrumentationTestRunner}
Empty test suite.

I also have the testPackageName specified in my defaultConfig in the build.gradle file. Am running Android Studio 0.4.6 on Ubuntu 12.0.4.

Any ideas?

Arid answered 28/2, 2014 at 22:16 Comment(2)
Try also setting the testInstrumentationRunner in defaultConfig.Throughput
include your complete project structure.Fashion
G
8

I had the same problem and could not find anything on the net. The answer was pretty simple: You have to edit the RunConfiguration of your test so that it is not a Android Test but a JUnit test.

So basically I did the following:

  1. Clicked "Edit Configuration"
  2. Clicked "Add new Configuration" and chose JUnit
  3. Inserted a name and the and chose "All in package" at Test kind.

That's it.

Glandule answered 30/4, 2014 at 12:2 Comment(1)
The problem occures for my tests which worked before, so not the best solution to recreate all run configurations againHarlot
T
7

I had the same problem, what happened to me was an error with the configurations of tests.

Be sure that you have the correct base configurations for testing like those below in the image:

enter image description here

If you don't have an Android Instrumented Tests configuration, you can create one from the yellow arrow.

PS: Moreover check that classes referenced by configurations really exists and the package name is correct

Taite answered 17/5, 2019 at 8:57 Comment(0)
L
6

I also get error like this and I added the following line in build.gradle file as shown in https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html and it's working. I hope it will also help you :-)

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Lsd answered 27/12, 2015 at 6:40 Comment(3)
Also add androidTestCompile 'com.android.support.test:rules:0.5' and androidTestCompile 'com.android.support.test:runner:0.5' to your dependencies of the module being tested. (If using shared libraries, these lines have to be in the shared library and the consuming app if the app is the one being tested—androidTestCompile includes don't get auto-included because of the "test" nature of the packaging mechanism.)Chace
It puzzled me because I already had the testInstrumentationRunner in gradle, and tests worked fine yesterday. Clean and Rebuild project did nothing. My trick: edited the line, and changed it back again so nothing changes but I can run gradle Sync now (you can just use the button in the menu). That solved it.Arabesque
I solve my problem by adding testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" in both module's build.gradle and application's build.gradle. In my case, tests are located in module.Cantlon
W
5

Basically three things to cross check:

  1. Make sure your have added testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

in your defaultconfig of build.gradle of that project/ module (in case of sub module)

  1. Make sure you have added following test compile dependencies:

    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support:support-annotations:25.2.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    
  2. Make sure you are adding your test files to the AndroidTest folders and not just to test folder.

Wilhelm answered 12/12, 2017 at 8:49 Comment(0)
P
3

I tried various methods but none fixed the issue for me. What I did is simply make a copy of the java test file into the same exact folder but with a different name and deleted the original.

I believe this issue was originally caused by moving my test class from the project test folder into the androidTest folder.

Preconcert answered 7/12, 2018 at 21:59 Comment(0)
L
1

I removed the entry android.support.test.runner.AndroidJUnitRunner from the Android Test configuration and it started to run the tests for me

Lierne answered 3/10, 2016 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.