2022-04-13

Cannot resolve method 'assertThat(int)'

I'm following the Test Navigation docs that has the following test:

@RunWith(AndroidJUnit4.class)
public class TitleScreenTestJava {

    @Test
    public void testNavigationToInGameScreen() {

        // Create a TestNavHostController
        TestNavHostController navController = new TestNavHostController(
            ApplicationProvider.getApplicationContext());

        // Create a graphical FragmentScenario for the TitleScreen
        FragmentScenario<TitleScreen> titleScenario = FragmentScenario.launchInContainer(TitleScreen.class);

        titleScenario.onFragment(fragment ->
                // Set the graph on the TestNavHostController
                navController.setGraph(R.navigation.trivia);

                // Make the NavController available via the findNavController() APIs
                Navigation.setViewNavController(fragment.requireView(), navController)
        );

        // Verify that performing a click changes the NavController’s state
        onView(ViewMatchers.withId(R.id.play_btn)).perform(ViewActions.click());
        assertThat(navController.currentDestination.id).isEqualTo(R.id.in_game);
    }
}

First of all, this gives

Cannot resolve symbol 'currentDestination'

After some digging, I find getCurrentDestination(). I also have to change id to getId() to fix a similar error.

Then I get

Cannot resolve method 'assertThat(int)'

What version of assertThat() should I import? I found 2 versions in JUnit, but neither takes only one parameter. Besides both are deprecated. org.hamcrest.MatcherAssert has 3 versions of assertThat(), but again, none take a single int or Integer parameter. So where do I find the right version of assertThat()?

Beyond that, what am I missing here with all these changes I seem to be needed to fix the example from the official android docs? Or is this example broken?



No comments:

Post a Comment