没有注册仪器!必须在注册工具下运行

时间:2018-11-08 17:16:51

标签: java android android-espresso

我无法在设备上运行测试并出现错误No instrumentation registered! Must run under a registering instrumentation.,这是我的测试类:

package com.pecode.itrustyou.ui.login;

import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;

import com.pecode.itrustyou.R;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.rule.ActivityTestRule;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@MediumTest
public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void enterInvalidPassword() {
        onView(ViewMatchers.withId(R.id.etEmail)).perform(typeText("email@gmail.com"), closeSoftKeyboard());
        onView(withId(R.id.etPassword)).perform(typeText("asdgfhj"), closeSoftKeyboard());
        onView(withText("The username and password combination is invalid")).check(matches(isDisplayed()));
    }
}

这是我的构建指令。我添加了所有测试依赖项,但没有任何效果。我已经完成了此处描述的所有操作:Run espresso in ActivityInstrumentationTestCase2,在默认配置中添加了AndroidJUnitRunner。

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

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

android {
    buildToolsVersion '28.0.3'
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.pecode.itrustyou"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 4
        versionName "0.1.1"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            buildConfigField "String", "BASE_URL", '"https://test-itrustyouservices.azurewebsites.net"'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "BASE_URL", '"https://api.itrustyou.io"'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            def formattedDate = new Date().format('dd.MM.yyyy')
            outputFileName = "ITrustYou-${formattedDate}.apk"
        }
    }
    dataBinding {
        enabled = true
    }

}
repositories {
    flatDir {
        dirs 'libs'
    }
}


dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    ///viewModel
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
    //recyclerView
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    //constraint layout
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-v4:28.0.0'
    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    //circleImageView
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    ///barcode
    implementation(name: 'LibPdf417Mobi', ext: 'aar')
    //ui
    implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    //eventbus
    implementation 'org.greenrobot:eventbus:3.1.1'
    //support
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-annotations:28.0.0'
    //CrashLytics
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
        transitive = true
    }

    //test
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'

    // Espresso dependencies
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
    androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0'
}

1 个答案:

答案 0 :(得分:1)

您必须在build.gradle(应用程序)中使用 ALL AndroidX依赖项

// Core library
androidTestImplementation 'androidx.test:core:1.1.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'

因此更改此行

androidTestImplementation 'com.android.support.test:runner:1.0.2'

对此

androidTestImplementation 'androidx.test:runner:1.1.1'

并且不要忘记在defaultConfig中使用AndroidX testInstrumentationRunner

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"