Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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


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


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.


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.


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.