I want test "Settings" option in toolbar. On click of "Settings" option, It will launch new activity and from toolbar title name I want to confirm is it launched successfully launched or not. Below is the code:- java code
@RunWith(AndroidJUnit4.class)
@SmallTest
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void intialize(){
}
@Test
public void testCanGoToSettings() {
onView(withId(R.id.toolbar))
.perform(navigateTo(R.id.action_settings));
String expectedNoStatisticsText = InstrumentationRegistry.getTargetContext()
.getString(R.string.action_settings);
onView(withId(R.id.toolbarSettings)).check(matches(withText(expectedNoStatisticsText)));
}
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/home_menu">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
I am getting below error.
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.xyz.android.control:id/toolbar
Kindly help, How I can implement this.