Я не могу запустить тест на своем устройстве и получаю сообщение об ошибке 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()));
    }
}

Вот мой градиент сборки. Я добавил все тестовые зависимости, но ничего не работает. И я сделал все, что описано здесь: Запустить эспрессо в 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'
}
9
Nazarii Moshenskiy 8 Ноя 2018 в 20:16

1 ответ

Лучший ответ

Вы должны использовать ВСЕ зависимости AndroidX в своем build.gradle (приложении)

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

И не забудьте использовать AndroidX testInstrumentationRunner в своем defaultConfig

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2
crubio 16 Мар 2019 в 15:23