无法解析方法 'assertThat(int)'

Cannot resolve method 'assertThat(int)'

我正在关注具有以下测试的 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);
    }
}

首先,这给出了

Cannot resolve symbol 'currentDestination'

经过一番挖掘,我找到了 getCurrentDestination()。我还必须将 id 更改为 getId() 以修复类似的错误。

然后我得到

Cannot resolve method 'assertThat(int)'

我应该导入什么版本的 assertThat()?我发现 2 versions in JUnit, but neither takes only one parameter. Besides both are deprecated. org.hamcrest.MatcherAssert 有 3 个版本的 assertThat(),但同样,none 采用单个 intInteger 参数。那么我在哪里可以找到 assertThat() 的正确版本?

除此之外,我似乎需要所有这些更改来修复官方 android 文档中的示例,我在这里遗漏了什么?还是这个例子坏了?

使用“assert”而不是“assertThat”。

private static boolean getBool(int i) {     return i == 0;   }
public static void main(String[] args) {
  // assert false; !will throw error
  assert true;// nothing will happen
  assert getBool(0);
  // assert getBool(1); !will throw error
}

这来自 AssertJ 图书馆。
Maven:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.22.0</version>
    <scope>test</scope>
</dependency>

AssertJ is a Java library that provides a rich set of assertions and truly helpful error messages, improves test code readability, and is designed to be super easy to use

// basic assertions
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);

documentation assertThat(int)

更新:
我更深入地查看了 android 文档。 看起来 Truth library (Maven repo) 被使用了。
根据 android 文档和建议,Truth 是首选库。它是 Jetpack documentation 的一部分。
Jetpack 是一套库,可帮助开发人员遵循最佳实践、减少样板代码并编写可跨 Android 版本和设备一致工作的代码,以便开发人员可以专注于他们关心的代码。


example of test with correct imports. Git
Documentation about assertThat(Integer)

AndroidX is an extension of Truth

Other extensions that are not part of the Truth project itself include:

  • AndroidX Test for testing Android types, like Intent

Setup project with AndroidX

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
    androidTestImplementation "androidx.test.ext:truth:$truthVersion"

真相与 AssertJ

Truth and AssertJ are very similar. This raises the question: Why did we create Truth? The reason is historical: AssertJ didn’t exist when we started Truth. By the time it was created, we’d begun migrating Google code to Truth, and we’d made some design decisions that would be difficult to retrofit onto AssertJ.