Issue
Recently, I've decided to update my Espresso code to use ActivityScenarioRule
instead of the deprecated ActivityTestRule
.
To do this, I've imported the following library into my project:
The final code using 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())
}
}
Whenever I run the UI tests, I get the following error:
Unresolved reference: ext
The error is coming from this line:
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'
}
Does anybody have any idea how to fix this? This import statement was added automatically by Android Studio, and I'm confused why it's saying that 'ext' is an unresolved reference. Any help would be greatly appreciated.
It's not that the code with ActivityTestRule
doesn't work, I just wanted to switch to a safer alternative which seems to be ActivityScenarioRule
- if I keep running into these problems I might need to revert back to the deprecated library.
If I can improve this question in any way, please let me know in the comments.
Solution
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
You have it imported as testImplementation
- it should be androidTestImplementation
.
How you declare the dependency depends on what kind of tests are going to use it. testImplementation
is for anything under test/
(usually unit tests that don't depend on the Android framework). Anything that does depend on the Android framework goes under androidTest/
and so would use androidTestImplementation
:
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'
Answered By - dominicoder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.