Issue
I wrote the code for testing the fragment. Wrote test methods testChangeSystemsMeasuresToMetric, testChangeSystemsMeasuresToImperial, checkFragmentOpen, but if you run them together, testChangeSystemsMeasuresToImperial fails with the androidx.test.espresso.AmbiguousViewMatcherException: 'is assignableible class class error.Problem views are marked with '**** MATCHES ****' below. At the same time, each tete completes successfully and if you run testChangeSystemsMeasuresToImperial and testChangeSystemsMeasuresToMetric together, then all tests will run successfully. What could be the problem?
class ProfileFragmentTest {
@Rule
@JvmField
val activityRule = ActivityTestRule(MainActivity::class.java)
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)
private val testProfile = Profile(SystemsMeasures.Metric, ObservableBoolean(true),
ObservableInt(0), ObservableDouble(0.0))
@Before
fun setUp() {
CurrentProfile.currentProfile = testProfile
onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT)))
.perform(DrawerActions.open());
onView(withId(R.id.nav_view))
.perform(NavigationViewActions.navigateTo(R.id.nav_profile))
}
@Test
fun checkFragmentOpen() {
onView(withId(R.id.profile_fragment)).check(matches(isDisplayed()));
}
@Test
fun testChangeSystemsMeasuresToImperial() {
val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.poundsMassUnit)
clickChangeSystemsMeasures(itemText)
assert(testProfile.systemMeasures == SystemsMeasures.Imperial)
}
@Test
fun testChangeSystemsMeasuresToMetric() {
testProfile.systemMeasures = SystemsMeasures.Imperial
val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.kilogramsMassUnit)
clickChangeSystemsMeasures(itemText)
assert(testProfile.systemMeasures == SystemsMeasures.Metric)
}
private fun clickChangeSystemsMeasures(itemText: String) {
val spinnerId: Int = R.id.spinner
onView(withId(spinnerId)).perform(click())
onData(allOf(`is`(instanceOf(String::class.java)), `is`(itemText))).perform(click())
}
}
Solution
I was able to solve my problem with orchestrator
build.gradle
dependencies{
androidTestUtil 'androidx.test:orchestrator:1.2.0'
}
android{
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
True, I still do not understand what the problem was, I will be grateful if you tell me why this problem arose.
Answered By - Destroyer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.