【问题标题】:Unresolved reference: ext when importing androidx.test.ext.junit.rules.ActivityScenarioRule未解决的参考:导入androidx.test.ext.junit.rules.ActivityScenarioRule时的ext
【发布时间】:2023-02-01 17:24:54
【问题描述】:

最近,我决定更新我的 Espresso 代码以使用 ActivityScenarioRule 而不是已弃用的 ActivityTestRule

为此,我将以下库导入到我的项目中:

使用ActivityScenarioRule的最终代码:

package com.realtomjoney.pyxlmoose.activities.main

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import com.realtomjoney.pyxlmoose.R
import org.junit.Rule
import org.junit.Test
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.runner.RunWith


@LargeTest
@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTestsWhenNewProjectIsCreated {
    @get:Rule
    var activityTestRule = ActivityScenarioRule(MainActivity::class.java)

    private fun createNewProject() {
        onView(withId(R.id.floatingActionButton)).perform(click())
        onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputEditText)).perform(ViewActions.replaceText("Untitled Project"))
        onView(withId(R.id.fragmentNewCanvas_spanCountTextInputEditText)).perform(ViewActions.replaceText("5"))
        onView(withId(R.id.fragmentNewCanvas_doneButton)).perform(click())
    }

    @Test
    fun uitest_fragmentNewCanvasProjectTitleTextInputEditText_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
        createNewProject()
        onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputEditText)).check(ViewAssertions.doesNotExist())
    }

    @Test
    fun uitest_fragmentNewCanvasProjectTitleTextInputLayout_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
        createNewProject()
        onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputLayout)).check(ViewAssertions.doesNotExist())
    }

    @Test
    fun uitest_fragmentNewCanvasSpanCountTextInputEditText_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
        createNewProject()
        onView(withId(R.id.fragmentNewCanvas_spanCountTextInputEditText)).check(ViewAssertions.doesNotExist())
    }

    @Test
    fun uitest_fragmentNewCanvasSpanCountTextInputLayout_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
        createNewProject()
        onView(withId(R.id.fragmentNewCanvas_spanCountTextInputLayout)).check(ViewAssertions.doesNotExist())
    }

    @Test
    fun uitest_fragmentNewCanvasDoneButton_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
        createNewProject()
        onView(withId(R.id.fragmentNewCanvas_doneButton)).check(ViewAssertions.doesNotExist())
    }
}

每当我运行 UI 测试时,我都会收到以下错误:

Unresolved reference: ext

错误来自这一行:

import androidx.test.ext.junit.rules.ActivityScenarioRule

build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    testOptions {
        unitTests.includeAndroidResources = true
    }

    defaultConfig {
        applicationId "com.realtomjoney.pyxlmoose"
        minSdk 27
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
//    implementation 'androidx.test:core-ktx:1.4.0'
    implementation 'com.google.android.material:material:1.5.0-beta01'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'androidx.test:core-ktx:1.4.0'
    testImplementation 'androidx.test.ext:junit-ktx:1.1.3'
    testImplementation 'org.robolectric:robolectric:4.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha03'
    androidTestImplementation 'androidx.test:rules:1.4.1-alpha03'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
}

我怎样才能解决这个问题?这个导入语句是由 Android Studio 自动添加的,我很困惑为什么它说“ext”是一个未解析的引用。

不是ActivityTestRule的代码没有工作,我只是想切换到一个更安全的替代方案,它似乎是 ActivityScenarioRule - 如果我继续遇到这些问题,我可能需要恢复到已弃用的库。

【问题讨论】:

  • 你的build.gradle 文件是什么样的?
  • @dominicoder 我编辑了我的问题以包含该文件。

标签: java android kotlin junit android-espresso


【解决方案1】:

呃,等一下,我的项目中已经包含了“androidx.test:core-ktx:1.4.0”的实现……被注释掉的只是一个重复的哈哈

您将它导入为 testImplementation - 它应该是 androidTestImplementation

如何声明依赖关系取决于将使用它的测试类型。 testImplementation 用于test/ 下的任何内容(通常是不依赖于 Android 框架的单元测试)。任何依赖于 Android 框架的东西都在 androidTest/ 下,所以会使用 androidTestImplementation

androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2016-12-14
    • 2020-05-27
    相关资源
    最近更新 更多