The problem is, that you try to match the list view itself with the instanceOf(ListView.class)
as argument for onData()
. onData()
requires a data matcher that matches the adapted data of the ListView
, not the ListView
itself, and also not the View
that Adapter.getView()
returns, but the actual data.
If you have something like this in your production code:
ListView listView = (ListView)findViewById(R.id.myListView);
ArrayAdapter<MyDataClass> adapter = getAdapterFromSomewhere();
listView.setAdapter(adapter);
Then the Matcher argument of Espresso.onData()
should match the desired instance of MyDataClass
.
So, something like this should work:
onData(hasToString(startsWith("ASDF"))).perform(click());
(You can use another Matcher using a method of org.hamcrest.Matchers
)
In case you have multiple adapter views in your activity, you can call ViewMatchers.inAdapterView()
with a view matcher that specifies the AdapterView like this:
onData(hasToString(startsWith("ASDF")))
.inAdapterView(withId(R.id.myListView))
.perform(click());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…