未解决的参考:导入时的分机 androidx.test.ext.junit.rules.ActivityScenarioRule
Unresolved reference: ext when importing androidx.test.ext.junit.rules.ActivityScenarioRule
最近,我决定更新我的 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
-如果我继续 运行 解决这些问题,我可能需要恢复到已弃用的库。
如果我能以任何方式改进这个问题,请在评论中告诉我。
Uhh, wait a second, I already have implementation 'androidx.test:core-ktx:1.4.0' included in my project... The commented out one is just a duplicate lol
您已将其导入为 testImplementation
- 它应该是 androidTestImplementation
。
如何声明依赖关系取决于将使用它的测试类型。 testImplementation
适用于 test/
下的任何内容(通常是不依赖于 Android 框架的单元测试)。任何依赖于 Android 框架的东西都在 androidTest/
下,因此将使用 androidTestImplementation
:
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'
最近,我决定更新我的 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
-如果我继续 运行 解决这些问题,我可能需要恢复到已弃用的库。
如果我能以任何方式改进这个问题,请在评论中告诉我。
Uhh, wait a second, I already have implementation 'androidx.test:core-ktx:1.4.0' included in my project... The commented out one is just a duplicate lol
您已将其导入为 testImplementation
- 它应该是 androidTestImplementation
。
如何声明依赖关系取决于将使用它的测试类型。 testImplementation
适用于 test/
下的任何内容(通常是不依赖于 Android 框架的单元测试)。任何依赖于 Android 框架的东西都在 androidTest/
下,因此将使用 androidTestImplementation
:
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'