Search code examples
androidandroid-studiogradlejunitpowermock

Can't run unit tests in Android Studio


I'm trying to make my unit tests work again for two days now, every time that I try to run them I get this error:

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

This happens trying to run both the test class, or just the methods. I've tried to clean all the test configurations and rebuild the project. Furthermore, I was able to find other people having the same problem, therefore, the proposed solutions were not effective to fix my issue.

This is my gradle file:

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

And the beginning of the class with the tests:

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

Thanks!

Update

I was able to fix partially the problem by changing the references in my AndroidMaifest file.

Therefore, changing the references from:

<activity android:name="com.myapp.MusicSelector.MusicSelectorActivity"/>

To:

<activity android:name=".MusicSelector.MusicSelectorActivity"/>

However, I can't create new tests or even rename my tests methods. For example, if I rename from:

@Test
public void testVideoListToTimedClips() throws Exception {

to:

@Test
public void testVideoListToTimedClips2() throws Exception {

I get the error:

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

Update 2

Trying things out, seems like Android Studio is keeping the reference to the an old version of the tests.

I changed the name of the test method from testVideoListToTimedClips to testVideoListToTimedClips2 resulting in a test class like that:

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

And when I run the test class, Android Studio runs the old method:

enter image description here


Solution

  • Are you using Android Studio 2.3.0-beta1? If so, there's a bug that causes your unit tests to not get built when you try to run them.

    You can work around the issue by first running Make project and then Run each time you want to run the unit tests.

    This issue is supposed to be fixed in the next canary/beta version of Android studio.

    Source: https://code.google.com/p/android/issues/detail?id=230688