I have been teaching myself programming with Kotlin in Android Studio and have gotten stuck on an example from the Pluralsight class "Android Apps with Kotlin: Tools and Testing". The course was created by Jim Wilson. Basically, I ran the exact code that the author ran and started getting the following errors while trying to match a spinnerCourses spinner view.
"E/ViewAssertions: 'with text: is "Android Programming with Intents"' check could not be performed because view 'with id: com.example.notekeeper:id/spinnerCourses' was not found."
And then:
"I/TestRunner: android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.example.notekeeper:id/spinnerCourses If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{efd885e VFED.VC.. ........ 0,0-1080,1457 #7f08006b app:id/listNotes}"
In order to diagnose the problem, I modified the code by adding several Log.i() calls and have also broken up the original onView() call into several lines to see where the problem occurs. Apparently, the matcher just can't seem to find the view in question. I double-checked and triple-checked the view spelling, and that is fine. I compared the code I had with the example code, and couldn't notice any reasons why there would be problems.
Here is the code, which has been slightly modified from the original for logging and debugging purposes. The last of my log entries I can find in the logcat is "before onView for spinner".
Running this in Android Studio 3.6.3, current Kotlin plugin version 1.3.72-release-Studio3.6-1. Running all this on Kubuntu 20.04 LTS (5.4.0-31-generic GNU/Linux).
package com.example.notekeeper
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import android.support.test.espresso.Espresso.*
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.action.ViewActions.*
import android.support.test.espresso.assertion.ViewAssertions.*
import android.util.Log
import org.hamcrest.CoreMatchers.*
@RunWith(AndroidJUnit4::class)
class NextThroughNotesTest {
private val tag = this::class.simpleName
@Rule @JvmField
var noteListActivity = ActivityTestRule(NoteListActivity::class.java)
@Test
fun nextThroughNotes() {
Log.i(tag, "entered the nextThroughNotes function")
val firstNote = allOf(instanceOf(NoteInfo::class.java), equalTo(DataManager.notes[0]))
onData(firstNote).perform(click())
for (index in 0..DataManager.notes.lastIndex) {
val note = DataManager.notes[index]
//Ivan, 5/21/2020: separate out onView spinner courses matching in order to debug
//the issue with the spinner view not being found
Log.i(tag, "before withId() function")
val viewMatchedById = withId(R.id.spinnerCourses)
Log.i(tag, "before matches() function")
val courseMatchedByTitle = matches(withSpinnerText(note.course?.title))
Log.i(tag, "before onView for spinner")
onView(viewMatchedById).check(courseMatchedByTitle)
Log.i(tag, "after onView for spinner")
Log.i(tag, "before onView for textNoteTitle")
onView(withId(R.id.textNoteTitle)).check(
matches(withText(note.title)))
Log.i(tag, "after onView for textNoteTitle")
Log.i(tag, "before onView for textNoteText")
onView(withId(R.id.textNoteText)).check(
matches(withText(note.text)))
Log.i(tag, "after onView for textNoteText")
Log.i(tag, "index check against the DataManager notes collection")
if (index != DataManager.notes.lastIndex)
onView(allOf(withId(R.id.action_next), isEnabled())).perform(click())
Log.i(tag, "end of index check agaonst DataManager collection")
}
onView(withId(R.id.action_next)).check(matches(not(isEnabled())))
Log.i(tag, "Exiting the nextThroughNotes function. Last statement in it...")
}
}