我正在跟踪Test Navigation docs名进行以下测试:
@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);
}
}
首先,这给了
无法解析符号"currentDestination"
经过一些挖掘,我找到了getCurrentDestination()
个.我还必须将id
改为getId()
以修复类似的错误.
然后我得到
无法解析方法"assertThat(int)"
我应该导入assertThat()
的哪个版本?我找到了2 versionsin JUnit,但它们都只接受一个参数.此外,这两种方法都不推荐.org.hamcrest.MatcherAssert
有3个版本的assertThat()
,但同样地,没有一个采用单个int
或Integer
参数.那么我在哪里可以找到正确的assertThat()
版本呢?
除此之外,我在这里遗漏了什么?这些变化似乎需要我来修复官方android文档中的示例?还是这个例子被打破了?