Test running failed: Unable to find instrumentation info for: ComponentInfo{} -- error trying to test in IntelliJ with Gradle

AndroidGradleAndroid Testing

Android Problem Overview


Everytime I try to run my tests the console says this:

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

I've been stuck on this for a while and the solutions I've seen online so far have not helped. My project structure is set up like this:

*Main Module -src *instrumentTest -java *main -java -manifest *build.gradle

My build.gradle file looks like this:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
compileSdkVersion 19
buildToolsVersion "19.1.0"

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 19
    versionCode 1
    versionName "2.1.0"
    testPackageName "login.test"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-   rules.txt'
    }
}

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/scandit.zip')
compile project(':pullToRefresh')
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1+'
compile 'org.springframework.android:spring-android-rest-template:1.0.1+'
compile 'org.json:json:20090211'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.1'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.3.1'
compile 'com.android.support:support-v4:19.1.+'
compile 'com.mcxiaoke.volley:library:1.0.+@aar'
androidTestCompile 'junit:junit:3.8'
}

Do you need to have a separate manifest for your tests directory? If so what would that look like?

Edit: I tried adding a manifest to my instrumentTest directory with no luck. Note that I could not get IntelliJ to resolve the targetPackage name, so it appears red.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.employeeappv2.employeeappv2.src.instrumentTest"
      android:versionCode="1"
      android:versionName="1.0.0">
<application>
    <uses-library android:name="android.test.runner" />
</application>
<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.employeeappv2.employeeappv2.src.main"/>
</manifest>

Android Solutions


Solution 1 - Android

I had the same error when I tried adding multiDexEnabled true to build.gradle.

I'm adding my experience here, because this is one of the first Google hits when searching with the ... Unable to find ... ComponentInfo ... error message.

In my case adding testInstrumentationRunner like here did the trick:

...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }
}

(I have com.android.tools.build:gradle:1.5.0)

Solution 2 - Android

I am using Android Studio 1.1 and the following steps solved this issue for me:

  1. In Run - Edit Configurations - Android Tests
    Specify instrumentation runner as android.test.InstrumentationTestRunner

  2. Then in the "Build variants" tool window (on the left), change the test artifact to Android Instrumentation Tests.

No testInstrumentationRunner required in build.gradle and no instrumentation tag required in manifest file.

Solution 3 - Android

When I created a new package, Studio created an ApplicationTest class. Using us.myname.mypackage as an example, the following directory structure was created:

app/[myPackage]/src/androidTest/java/us/myname/mypackage/ApplicationTest.class

Initially it worked out of the box. It quit working after I installed product flavors. I subsequently made the following changes to build.gradle:

defaultConfig {
   ...
   testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

some prefer

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

(With my current configuration, I have to use ...InstrumentationTestRunner when in debug, and AndroidJUnitRunner while using release build type.)

The above configuration only works with the debug build type. If you wish to use it with release or with a custom build type, you can include the following in build.gradle:

buildTypes {
     release {
         ...
     }
 debug {
     ...
 }
}
testBuildType "release"

In the Build Variants tab on the lower left side of Studio, make sure you have Android Instrumentation Tests and the correct buildType selected.

Solution 4 - Android

I had to do a combination of VikingGlen's answer, Liuting's answer, and this answer. This answer works for Android Studio version 2.1.

  1. Run -> Edit Configurations... -> General -> Specific instrumentation runner (optional): "android.support.test.runner.AndroidJUnitRunner"

  2. In build.gradle (the one with all your dependencies), put this:

defaultConfig {
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} 

Then it could run tests of this type:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void login() {
        //Open Drawer to click on navigation.
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(open()); // Open Drawer
    }
}

Solution 5 - Android

For what it's worth, AS 2.3 got hung up when i created a custom test runner after using the regular test runner. I got the same error as posted in the question.

Deleting the Debug Configurations for ALL Android Instrumented Tests and rebuilding fixed it. I believe the problem lied in the fact you no longer can choose a custom runner in the Debug Configurations because it's most likely built in via gradle.

Solution 6 - Android

So the main problem was that when I created an androidTest folder under /src/, it wasn't being picked up by IntelliJ as a source folder for testing (java subdirectory should turn green). I was using IntelliJ 13.0.3 and after upgrading to 13.1.3, all of my troubles went away.

*Note: do not try to add a manifest to your androidTest folder, the Gradle docs specifically state that the manifest should be auto-generated when you create the androidTest folder. The problem for me was that the file wasn't being generated as androidTest wasn't being recognized by IntelliJ/Gradle, thus throwing the no instrumentation error.

Solution 7 - Android

I had this problem and fixed it by going to Run -> Edit Configurations -> Green '+' button at the top left -> JUnit

From there, set the 'use the classpath mod...' to 'app' (or your default app name, which is the one that appears to the left of the run (play button) when you run the app)

Finally, put your test class name in the 'class:' textbox. Click apply and okay.

At this point, if the test class doesn't have other errors, it should work.

Solution 8 - Android

This is what I noticed in my project, in my app(main) module build.gradle I had the following buildType configuration

buildTypes {
        debug {
            multiDexEnabled true
        }

        mock {
            initWith(buildTypes.debug)
        }
    }
testBuildType "mock"

When I used AndroidJUnitRunner as the test runner(both from Android Studio) and as testInstrumentationRunner in build.gradle, tests ran without hitch.

In a submodule that had multiDexEnabled true as defaultConfig

defaultConfig {
    multiDexEnabled true
    ....
}

I ran into the problem of

Test running startedTest running failed: Unable to find instrumentation info for:{mypackage.x.y/android.support.test.runner.AndroidJUnitRunner"}

when I specified AndroidJUnitRunner in IDE and the submodule build.gradle. And this was fixed by specifying MultiDexTestRunner as the test runner in IDE/build.gradle.

>To summarize, Use MultiDexTestRunner to run tests when multiDexEnabled true is specified in build.gradle, else use AndroidJUnitRunner as the test runner.

Solution 9 - Android

Make sure the app has been uninstalled for all users.

Go to settings -> apps (all apps) -> If your app is there then tap on it -> menu -> uninstall for all users.

My issue was that the app was at one point uninstalled, however still on the device; meaning it was not uninstalled for all users. (Another issue, not sure how to resolve. I'm the only user on my device)

Because of this, the app wouldn't re-install, and the test suite had nothing to run against.

Solution 10 - Android

The solution for my problem is to change the method name from

@Test
public void test() {
    ...
}

to

@Test
public void testSomething() {
    ...
}

Hope it helps someone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnjthomaView Question on Stackoverflow
Solution 1 - AndroidPeter LambergView Answer on Stackoverflow
Solution 2 - AndroidLiutingView Answer on Stackoverflow
Solution 3 - AndroidVikingGlenView Answer on Stackoverflow
Solution 4 - AndroidRock LeeView Answer on Stackoverflow
Solution 5 - AndroidLEOView Answer on Stackoverflow
Solution 6 - AndroidnjthomaView Answer on Stackoverflow
Solution 7 - AndroidDaniel View Answer on Stackoverflow
Solution 8 - AndroidAgnitView Answer on Stackoverflow
Solution 9 - AndroidSababadoView Answer on Stackoverflow
Solution 10 - AndroidDino TwView Answer on Stackoverflow