android - 在 Android Studio 中运行仪器测试的问题;未找到类 "android.test.runner.AndroidJUnitRunner"

标签 android android-studio junit android-espresso

我正在尝试在 android studio 中运行一个测试用例,该测试用例应该打开抽屉导航,单击菜单项,然后检查它是否导航到该项目。

当我尝试运行测试时,出现以下错误

Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.

Logcat显示;

-02-04 13:41:17.703 10121-10121/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.damhan.dublinbusassist, PID: 10121
    java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.damhan.dublinbusassist.test/android.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.test.runner.AndroidJUnitRunner" on path: DexPathList...

这是我的测试用例:

package com.damhan.dublinbusassist;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.view.Gravity;

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

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.contrib.DrawerActions.open;
import static android.support.test.espresso.contrib.DrawerMatchers.isClosed;
import static android.support.test.espresso.contrib.NavigationViewActions.navigateTo;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class NavigationDrawerTest {
    @Test
    public void clickRouteNavigationItem_ShowsRouteScreen() {
        // Open Drawer to click on navigation.
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(open()); // Open Drawer

        onView(withId(R.id.nav_view))
                .perform(navigateTo(R.id.route_menu));


        String expectedNoStatisticsText = InstrumentationRegistry.getTargetContext()
                .getString(R.string.routePageText);
        onView(withId(R.id.route_menu)).check(matches(withText(expectedNoStatisticsText)));
    }
}

我的build.grade如下;

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.damhan.dublinbusassist"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    // Required -- JUnit 4 framework
    testImplementation 'junit:junit:4.12'
    // Optional -- Mockito framework
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support:support-annotations:27.1.1'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'

}

我对编写测试用例非常陌生,所以我意识到可能会出现错误,并且我从示例 google android-testing github 中获取,因此某些内容当前可能命名不正确,但我希望它至少跑!

最佳答案

它是android.support.test.runner.AndroidJUnitRunner。注意支持部分

关于android - 在 Android Studio 中运行仪器测试的问题;未找到类 "android.test.runner.AndroidJUnitRunner",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54517907/

相关文章:

android - 了解手机上的 Bootstrap 布局

Android 应用程序添加地理围栏并在同一服务中接收 Intent

android-studio - 如何关闭 Android Studio 中的通知?

android - 如何删除这些不需要的权限

java - Android:按下弹出菜单项时的背景颜色

javascript - 如何使用 Cordova 将电话号码发送到 SIP 客户端?

java - IntelliJ 2017 中没有代码覆盖率

java - DAO 单元测试期间恢复数据的策略是什么?

java - 如何使用 Mockito 模拟可分页对象?

android - 无法从 Activity 获取数据到 Service