Android: Instrumentation run failed due to 'java.lang.ClassNotFoundException': No tests found
Asked Answered
P

7

29

I try to run a instrumentation test via "gradlew connectedAndroidTest". I get following error:

Tests on GT-I9305 - 4.3 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

com.android.builder.testing.ConnectedDevice > hasTests[GT-I9305 - 4.3] FAILED 
No tests found.                              
:mobile:connectedAndroidTest FAILED 

This is the output with --stacktrace option:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':mobile:connectedAndroidTest'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:305)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
        at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
        at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
        at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:68)
        at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)
        at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
        at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
        at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:33)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:130)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: org.gradle.api.GradleException: There were failing tests. See the report at: file:///D:/myapp/mobile/build/outputs/reports/androidTests/connected/index.html
        at com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask.runTests(DeviceProviderInstrumentTestTask.groovy:92)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:218)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:200)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:579)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:562)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
        ... 47 more

My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >

    <instrumentation
        android:targetPackage="com.example.myapp"
        android:name="android.test.InstrumentationTestRunner" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="com.example.myapp.MyApplication">
        <activity
            android:name="com.example.myapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

My build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'robolectric'

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        androidTest {
            setRoot('src/androidTest')
        }
        test {
            setRoot('src/test')
        }
    }
}

android {
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
}
configurations {
    apt
}

apt {
    arguments {
        resourcePackageName android.defaultConfig.packageName
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}

ext {
    daggerVersion = '1.2.2';
    androidAnnotationsVersion = '3.2';
    robobindingVersion = '0.8.9';
    jodatimeVersion = '2.5.1';
    ormliteVersion = '4.48';
    ottoVersion = '1.3.5';
    commonsioVersion = '2.0.1';
    playservicesVersion = '6.1.71';
    supportv4Version = '21.0.2';
    javaxinjectVersion = '1';
    junitVersion = '4.11';
    robolectricVersion = '2.4';
}

dependencies {
    androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
    androidTestCompile 'com.google.guava:guava:14.0.1'
    androidTestCompile 'com.squareup.dagger:dagger:1.2.2'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'

    androidTestCompile("junit:junit:${junitVersion}") {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile("org.robolectric:robolectric:${robolectricVersion}") {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }

    androidTestCompile 'com.squareup:fest-android:1.0.+'

    apt "org.robobinding:codegen:$robobindingVersion"
    compile("org.robobinding:robobinding:$robobindingVersion:with-dependencies") {
        exclude group: 'com.google.guava', module: 'guava'
    }
    apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "org.androidannotations:androidannotations-api:${androidAnnotationsVersion}"
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"
    compile fileTree(dir: 'libs', include: ['*.jar'])
    wearApp project(':wear')
    compile project(':javamail')
    compile "com.google.android.gms:play-services:${playservicesVersion}"
    compile "com.android.support:support-v4:${supportv4Version}"
    compile "com.squareup:otto:${ottoVersion}"
    compile "javax.inject:javax.inject:${javaxinjectVersion}"
    compile "com.j256.ormlite:ormlite-core:${ormliteVersion}"
    compile "com.j256.ormlite:ormlite-android:${ormliteVersion}"
    compile group: 'commons-io', name: 'commons-io', version: "${commonsioVersion}"
    compile "net.danlew:android.joda:${jodatimeVersion}"
}

apply plugin: 'idea'
idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}


android.applicationVariants.all { variant ->
    def aptOutput = file("${project.buildDir}/generated/source/apt/${variant.dirName}")
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"

    variant.javaCompile.doFirst {
        println "*** compile doFirst ${variant.name}"
        aptOutput.mkdirs()
        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    }
}

My test class:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public MainActivityTest() {
        super(MainActivity.class);
    }

    public void testSomething() {
        assertTrue(true);
    }

}    

Any idea?

Prater answered 1/12, 2014 at 17:40 Comment(4)
The answer I posted before seemed to only fix it for 5.0 devices, I added how I resolved fixing it for pre-5.0 devices after I found out it didn't work on those. Seemed you were also running 4.3Urgent
@Urgent I have this problem too. I could not run Espresso 2 tests on any device or emulator with API < 21.Porett
Check out the edit I made to the original answer, that should solve it for devices with API < 21.Urgent
@Urgent That wasn't the only cause for me, but it was the right direction. Thanks.Porett
U
22

I just found myself with the same problem, and resolved it by updating to espresso 2.0

Replace the testInstrumentationRunner in your build.gradle file

 testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner

to

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

and update the new dependencies to 2.0

androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')

to

androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

You also have to change the namespace in your tests, but you will notice when running them.

Full explanation: https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions

Edit:

Seems I was too soon in answering, the answer I posted only fixed it on 5.0 devices. Below is what fixed it on older devices:

Exclude javax.inject from the new dependency by changing:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

to

androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
    exclude group: 'javax.inject'
}

source: Dagger code giving NoClassDefFoundError when Espresso Tests are run, normal run works fine

Urgent answered 14/1, 2015 at 16:51 Comment(1)
Thank you so much for this!Donne
R
6

I got this problem when I wrote a test in Kotlin and used spaces in the method name. It crashes with the error message: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner"

In Android Studio: No tests where found

Broken Test: spaces in method names, doesn't work on Android

class RandomTest {

    @Test
    fun `do not use spaces it will not work on Android devices or emulators`() {
        assertThat(2 + 2).isEqualTo(4)
    }
}

Solution: removed spaces, everything compiles normally

class RandomTest {

    @Test
    fun write_method_names_java6_compatible_without_spaces() {
        assertThat(2 + 2).isEqualTo(4)
    }
}

This could happen because I disabled the Inspection "Illegal Android Identifier" so it doesn't annoy me when writing JVM tests

Roderica answered 13/1, 2017 at 11:6 Comment(0)
I
5

The problem for me was adding the test in Kotlin, but not having Kotlin support previously. So I need to put in to my gradle:

apply plugin: 'kotlin-android'

Then delete the test configuration and add it again.

Invar answered 16/4, 2019 at 4:44 Comment(0)
L
2

I just ran into this issue when after some refactoring I tried to run test from the console.

The solution was to fix the AndroidManifest.xml, as it contained references to no longer existing activities and other classes.

Sure thing, the project will compile with a bad manifest, but when you try to run it, the device won't find the defined classes. Thus the ClassNotFoundException.

Lowboy answered 28/1, 2015 at 19:50 Comment(0)
P
2

I had faced similar issue. In my project there were multiple modules and multidex was enabled. For me the problem was in one particular module multidex enable option (multiDexEnabled true) was missing.

In main module (app) the build.gradle was like following:

...
multiDexEnabled true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
...

In another module (on which app is dependent on), the build.gradle was missing the line "multiDexEnabled true". Adding this line resolved the issue.

Potentiometer answered 21/11, 2016 at 14:19 Comment(0)
F
2

Copying the answer from Original Answer.

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.

Fallacious answered 24/11, 2016 at 10:27 Comment(0)
L
0

I solved this by turning off the Proguard.

Lyophilize answered 14/10, 2018 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.