AndroidJUnit4 test not found

13,024

Solution 1

You may not have something in your Gradle dependencies. Did you declared properly com.android.support.test:runner (optionally com.android.support.test:rules)?

Please, take a look at my build.gradle file:

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

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
    }
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName "com.piotr.testexample"
    }
}

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    useLibrary 'org.apache.http.legacy'

    //For building with Travis CI
    lintOptions {
        abortOnError false
    }

    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

    defaultConfig {
        applicationId "com.piotr.testexample"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

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

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    ext.JUNIT_VERSION = '4.12'
    ext.AA_VERSION = '4.0.0'
    ext.SUPPORT_VERSION = '23.3.0'
    ext.ESPRESSO_VERSION = '2.2.2'

    apt "org.androidannotations:androidannotations:$AA_VERSION"
    compile "org.androidannotations:androidannotations-api:$AA_VERSION"

    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile "junit:junit:$JUNIT_VERSION"
    testCompile("org.robolectric:robolectric:3.0") {
        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'
    }

    compile "com.android.support:appcompat-v7:$SUPPORT_VERSION"
    compile "com.android.support:design:$SUPPORT_VERSION"
    compile "com.android.support:cardview-v7:$SUPPORT_VERSION"
    compile "com.android.support:recyclerview-v7:$SUPPORT_VERSION"

    compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'

    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.picasso:picasso:2.5.2'

    androidTestCompile "com.android.support:support-annotations:$SUPPORT_VERSION"
    androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION"
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION"
    /**
     * AccessibilityChecks
     * CountingIdlingResource
     * DrawerActions
     * DrawerMatchers
     * PickerActions (Time and Date picker)
     * RecyclerViewActions
     */
    androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'support-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
    }

    compile "junit:junit:${JUNIT_VERSION}"
}

EDIT: The problem is with annotation, please do one of these solutions:

  • delete @RunWith(AndroidJUnit4.class)
  • change @RunWith(AndroidJUnit4.class) with @RunWith(JUnit4.class)

It seems that there's no more AndroidJUnit4.class in test packages.

Solution 2

make sure you set buildType on debug as mentioned in : https://stackoverflow.com/a/37371171/5175944

Share:
13,024
Lemao1981
Author by

Lemao1981

Updated on June 22, 2022

Comments

  • Lemao1981
    Lemao1981 about 2 years

    I annotated my class like here

    @RunWith(AndroidJUnit4.class)
    public class WorkdayProviderTest
    

    Futhermore, annotated also my test method like this

    @Test
    public void insert_dataInsertsCorrectly()
    

    Finally, configured my build.gradle defaultConfig and dependencies like this

    defaultConfig {
        applicationId 'com.jueggs.workinghours'
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    
    androidTestCompile "junit:junit:4.12"
    androidTestCompile "com.android.support:support-annotations:23.3.0"
    androidTestCompile "com.android.support.test:runner:0.4.1"
    androidTestCompile "com.android.support.test:rules:0.4.1"
    androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.1"
    androidTestCompile "com.android.support.test.espresso:espresso-contrib:2.2.1"
    androidTestCompile "org.hamcrest:hamcrest-library:1.3"
    

    and configured the test run like this

    enter image description here

    and it´s telling me

    No tests were found
    
    Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
    

    How to fix this issue?

  • piotrek1543
    piotrek1543 about 8 years
    delete @RunWith(AndroidJUnit4.class) and run test. You can also change @RunWith(AndroidJUnit4.class) with @RunWith(JUnit4.class) and it should run.
  • Lemao1981
    Lemao1981 about 8 years
    did not solve the problem, in both cases cannot find any test. in the 0.4.1 version the class AndroidJUnit4 exists
  • Lorenzo Camaione
    Lorenzo Camaione about 8 years
    I'm sorry. I had and solved this problem in this way with Espresso Instrumentation tests. Anyway in Unit Test I use Robolectric. If you will feel stuck give it a try!
  • Lorenzo Camaione
    Lorenzo Camaione about 8 years
    In android studio, in Build Variants, have you switched from instrumented tests to unit tests?
  • piotrek1543
    piotrek1543 about 8 years
    update espresso to 2.2.2 and test-runner, test-rules to 0.5 and delete annotations. Still problem?
  • OhhhThatVarun
    OhhhThatVarun almost 5 years
    @RunWith(JUnit4.class) Worked for me