当前位置:  开发笔记 > Android > 正文

无法在Android Studio中运行单元测试

如何解决《无法在AndroidStudio中运行单元测试》经验,为你挑选了1个好方法。

我正在尝试让我的单元测试再次工作两天,每次我尝试运行它们时都会出现此错误:

Process finished with exit code 1
Class not found: "XXXXX.XXXXX.XXXXX.XXX"Empty test suite.

尝试运行测试类或仅运行方法时会发生这种情况.我试图清理所有测试配置并重建项目.此外,我能够找到其他人遇到同样的问题,因此,提出的解决方案无法解决我的问题.

这是我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.1'

    defaultConfig {
        applicationId "XXXXXXXXXX"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
}

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // The Fabric Gradle plugin uses an open ended version to react
        // quickly to Android tooling updates
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.4.0'
    }
}

apply plugin: 'io.fabric'
apply plugin: 'me.tatarka.retrolambda'

ext {
    supportLibVersion = '25.1.0'  // variable that can be referenced to keep support libs consistent
    jomlVersion = '1.8.4'
}

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:support-v4:${supportLibVersion}"
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar')
    {
        transitive = true;
    }
    compile project(':circular-slider')
    compile project(':gpuimage-plus')
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'net.protyposis.android.mediaplayer:mediaplayer:4.2.2-rc1'
    compile 'net.protyposis.android.mediaplayer:mediaplayer-dash:4.2.2-rc1'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.writingminds:FFmpegAndroid:0.3.2'
    compile 'com.makeramen:roundedimageview:2.3.0'
    compile 'io.reactivex:rxjava:1.2.4'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.minimize.android:rxrecycler-adapter:1.2.2'
    compile "org.joml:joml:${jomlVersion}"
    provided 'org.glassfish:javax.annotation:10.0-b28'
    compile 'org.jetbrains:annotations-java5:15.0'
    compile 'com.annimon:stream:1.1.3'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-api-mockito:1.6.5'
    testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.5'
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.5'
    testCompile 'org.powermock:powermock-module-junit5:1.6.5'
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
    compile 'com.android.support:multidex:1.0.1'
}

并且测试类的开头是:

@PrepareForTest({XXXXXX.class, XXXXXX.class, XXXXXX.class, XXXXXX.class})
@RunWith(PowerMockRunner.class)
public class VideoPlayerModelUnitTest {

谢谢!

更新

我能够通过更改AndroidMaifest文件中的引用来部分解决问题.

因此,更改引用来自:


至:


但是,我无法创建新测试甚至重命名我的测试方法.例如,如果我重命名为:

@Test
public void testVideoListToTimedClips() throws Exception {

至:

@Test
public void testVideoListToTimedClips2() throws Exception {

我收到错误:

java.lang.Exception: No tests found matching Method testVideoListToTimedClips2(io.collect.collect.functional.VideoPlayerModelIntegrationTest) from org.junit.internal.requests.ClassRequest@15eb5ee5

更新2

尝试一下,似乎Android Studio保留了对旧版本测试的引用.

我将测试方法的名称更改testVideoListToTimedClipstestVideoListToTimedClips2导致类似的测试类:

@PrepareForTest({VideoPlayerModel.class, MediaPlayer.class, Common.class})
@RunWith(PowerMockRunner.class)
public class testVideoPlayerModelIntegration {
    @Test
    public void testVideoListToTimedClips2() throws Exception {

当我运行测试类时,Android Studio会运行旧方法:

在此输入图像描述



1> starkej2..:

您使用的是Android Studio 2.3.0-beta1吗?如果是这样,那么当您尝试运行它们时,会出现导致单元测试无法构建的错误.

您可以通过首先运行Make project然后Run每次要运行单元测试来解决此问题.

这个问题应该在Android工作室的下一个canary/beta版本中修复.

资料来源:https: //code.google.com/p/android/issues/detail?id = 230688

推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有