无法解析方法“assertThat(int)”
我正在关注测试导航文档具有以下测试:
@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()
以修复类似的错误。
然后我得到
无法解析方法'断言(int)'
我应该导入哪个版本的assertthat()
?我发现 2个版本 在junit 中,但两者都不采用一个参数。此外,两者都被弃用。 org.hamcrest.matcherass.matcherassert.matcherassert.matcherassert
有3个版本的assertthat()
,但同样,没有一个int
或integer
参数。那么,我在哪里可以找到正确的版本surstThat()
?
除此之外,我似乎需要在这里进行所有这些更改,我似乎需要从官方的Android文档中修复示例?还是这个示例打破了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是来自 AssertJ 库。
Maven:
请参阅 文档 assertThat (整数)
更新:
我已经更深入地审查了 android 文档。
看起来像真相库 (Maven 存储库)。
根据 Android 文档和建议,Truth 是首选库。它是 Jetpack 文档的一部分。
Jetpack 是一套库,可帮助开发人员遵循最佳实践、减少样板代码并编写跨 Android 版本和设备一致运行的代码,以便开发人员可以专注于他们关心的代码。
请参阅使用正确导入的测试示例。 Git
有关文档assertThat(Integer)
AndroidX 是 Truth 的扩展
使用 AndroidX 设置项目
Truth 与 AssertJ
This is from AssertJ library.
Maven:
See documentation assertThat(int)
Update:
I have reviewed more deeply android docs.
Looks like Truth library (Maven repo) is used.
Truth is the preferred library according to android docs and recommendations. It is part of Jetpack documentation.
Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.
See example of test with correct imports. Git
Documentation about assertThat(Integer)
AndroidX is an extension of Truth
Setup project with AndroidX
Truth vs. AssertJ
使用“断言”而不是“断言”。
Use “assert” than “assertThat”.