类型Android Junit4不存在异常

时间:2018-08-14 00:54:46

标签: android kotlin android-gradle junit4

我有一个Android项目,我正在尝试使用AndroidStudio运行Instrumentation测试,但是当我尝试为测试运行整个类文件时,却出现了如下异常:

java.lang.TypeNotPresentException: Type android.support.test.runner.AndroidJUnit4 not present
Caused by: java.lang.ClassNotFoundException: android.support.test.runner.AndroidJUnit4
有趣的是,我可以在Instrument Test类中运行单个方法,而不是整个类。我的应用程序build.gradle文件如下:

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'
}

InstrumentTest类如下:

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() {

    }
}

非常感谢您的帮助/指导。

6 个答案:

答案 0 :(得分:9)

这是由于Android Studio尝试将检测测试作为JUnit测试执行而引起的。

我必须转到“编辑配置...”,然后从Android JUnit组中删除该配置,然后手动将其添加到“ Android Instrumented Tests”组中。在此之后,我毫无疑问地执行测试。

答案 1 :(得分:3)

希望有人可以为您提供更好的答案,但我却遇到了同样的问题。我做了以下事情:

  • 构建>清理项目,
  • 在模拟器上运行应用程序

完成此操作后,我能够回到可以再次运行测试的状态。

答案 2 :(得分:1)

如果有人在编辑@Bohsen answe以上的配置时遇到问题,该写步骤 r

  1. 在Android Studio中,单击Run > Edit Configurations(如果在Windows上则按alt + shift + f10)以打开配置编辑器。
  2. 窗口的左侧面板显示配置及其组。
    • Android JUnit组
    • Android检测测试

如果您的测试是仪器测试,请从任何JUnit测试组中将其删除。

如果您的测试是JUnit测试,请从任何工具测试组中将其删除。

例如,我的测试(DBTest.kt)出现在 Android JUnit和Instrumented组的部分中。因此,我从JUnit组中删除并再次运行它,它工作正常。

答案 3 :(得分:0)

我与:

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

在从设备上手动删除该应用程序并再次运行测试之前,我尝试了很多事情。希望这会有所帮助!

答案 4 :(得分:0)

对我来说,这实际上是由互联网连接问题引起的。关闭并重新打开wifi并修复似乎可以修复。

答案 5 :(得分:0)

在模拟器上进行测试时遇到了这个问题。

我通过从git存储库下载新的项目副本来修复它。