dimanche 24 janvier 2016

Using JUnit @BeforeClass causes Android Studio to not run any tests at all

I have an issue using the JUnit4 @BeforeClass annotation in instrumented Android unit test (I'm using the Espresso GUI testing library). As soon as I add a test with the @BeforeClass annotation, Android Studio 1.5.1 does not run any tests at all but instead just prints "Empty test suite". I'm not using a test suite. I searched this site and the web but couldn't find a solution. I thought that it might be a problem that the code, which is called within the @BeforeClass method actually fails (TDD), but this error even occurs when code, which is working in normal test cases, is put in the @BeforeClass annotated method.

Thank you.

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.not;


@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> menuActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    private static void checkSignBrowserIsDisplayed() {
        onView(withText(R.string.sign_browser)).check(matches(isDisplayed()));
    }

    @BeforeClass
    public static void checkSignBrowserIsDisplayedOnAppStartup() {
        checkSignBrowserIsDisplayed();
    }

build.app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "foo"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // required if you want to use Mockito for Android instrumentation tests - not needed now.
    // androidTestCompile 'org.mockito:mockito-core:1.+'
    // androidTestCompile "com.google.dexmaker:dexmaker:1.2"
    // androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

Test output

Running tests
Test running startedFinish
Empty test suite.

Aucun commentaire:

Enregistrer un commentaire