50

I have an Android project that I am trying to run Instrumentation tests for using AndroidStudio, but when I try to run the whole class file for the tests, I get the exceptions as stated below :

java.lang.TypeNotPresentException: Type android.support.test.runner.AndroidJUnit4 not present
Caused by: java.lang.ClassNotFoundException: android.support.test.runner.AndroidJUnit4

Interestingly, I can run individual methods in this Instrument Test class, just not the whole class. My build.gradle file for the app is below:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    lintOptions {
        abortOnError false
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.sarpuner.journal"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '27.0.3'
}

repositories {
    maven { url "https://dl.bintray.com/ijabz/maven" }
}

dependencies {
    androidTestImplementation 'com.android.support:support-annotations:27.1.1'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'dev.dworks.libs:volleyplus:0.1.4'
    implementation 'com.google.code.gson:gson:2.8.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.jsoup:jsoup:1.11.3'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1'
    implementation 'net.jthink:jaudiotagger:2.2.3'
    api 'com.google.guava:guava:26.0-android'
}

And the InstrumentTest class is as follows:

package com.sarpuner.journal

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import android.util.Log    
import org.junit.Test
import org.junit.runner.RunWith    
import org.junit.Assert.*
import java.io.File

private const val INSTRUMENT_TAG = "InstrumentTest"

// TODO: There is a problem here with the class run!

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    private val appContext = InstrumentationRegistry.getTargetContext()

    @Test
    fun useAppContext() {
        // Context of the app under test.
        assertEquals("com.sarpuner.journal", appContext.packageName)
    }

    @Test
    fun downloadAndSaveAudio() {
        val url = "http://telechargement.rfi.fr/rfi/francais/audio/jff/201808/journal_francais_facile_20h00_-_20h10_tu_20180809.mp3"
        val name = "09-08-2018.mp3"
        val f = File(appContext.filesDir, name)
        Log.d(INSTRUMENT_TAG, f.absolutePath)
        downloadAudio(url, f)
    }

    @Test
    fun downloadAndSaveTranscript() {
        val url = "https://savoirs.rfi.fr/fr/apprendre-enseigner/langue-francaise/journal-en-francais-facile-09082018-20h00-gmt"
        val name = "09-08-2018.txt"
        val f = File(appContext.filesDir, name)

        downloadText(url, f)
    }

    @Test
    fun addLyricsToAudio() {

    }
}

Any help/guidance is much appreciated, thank you.

6 Answers 6

113

This is caused by Android Studio trying to execute an instrumentation test as a JUnit test.

I had to go to 'Edit Configurations...' and delete the configuration from Android JUnit group and manually add to the 'Android Instrumented Tests' group. After this I had no problem executing the tests.

In Android Studio, click Run > Edit Configurations.

https://developer.android.com/studio/run/rundebugconfig

Sign up to request clarification or add additional context in comments.

6 Comments

This is correct and should be accepted as a correct answer.
@Bohsen How to do that, any steps ?
Why does Android Studio continue to try to add it to the JUnit tests instead of instrumented tests though? I don't want to have to manually adjust configuration for every test.
@FutureShocked in my case the reason was that the device was not recognized properly. I had some general usb recognition issue with my device. Additionally some phones ask for permission to allow access from the PC
I continue to face this issue, and pretty strictly use an emulator for instrumented testing.
|
10

Writing steps if someone facing issue while edit configuration for above @Bohsen's answer

  1. In Android Studio, click Run > Edit Configurations (or press alt + shift + f10 if on Windows) to open the configuration editor.
  2. The left panel of the window shows configurations and their groups.
    • Android JUnit group
    • Android instrumented tests

If your test is an instrumentation test, delete it from any JUnit tests groups.

If your test is a JUnit test, delete it from any instrumentation test groups.

For example, my test (DBTest.kt) was appearing in both Android JUnit and Instrumented group's section. So I deleted from JUnit group and ran it again, it was working fine.

Comments

4

Hopefully someone can give you a better answer as to why, but I had this exact same problem. I did the following:

  • Build > Clean Project,
  • Ran the app on the emulator

This done, I was able to get back into a state where I could run the tests again.

2 Comments

Android Studio unit tests are flaky as hell. I've had to clear caches and clean builds at almost every step. Makes TDD very tedious.
0

I had the same with:

Type androidx.test.ext.junit.runners.AndroidJUnit4 not present

I tried a lot of things before manually removing the App from the device and running the test again. Hope this helps!

Comments

0

For me this actually seemed to be caused by an internet connection issue. Turning off and on wifi fixed and rebuilding seemed to fix.

Comments

0

I got this issue when I did my tests on the emulator.

I fixed it by downloading a new project copy from the git repository.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.