无法解析方法“assertThat(int)”

发布于 2025-01-20 08:45:36 字数 2432 浏览 4 评论 0原文

我正在关注测试导航文档具有以下测试:

@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(),但同样,没有一个intinteger参数。那么,我在哪里可以找到正确的版本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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2025-01-27 08:45:36

这是来自 AssertJ 库。

Maven

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

AssertJ 是一个 Java 库,提供了丰富的断言和
真正有用的错误消息,提高了测试代码的可读性,并且
设计得非常易于使用

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

请参阅 文档 assertThat (整数)

更新:

我已经更深入地审查了 android 文档。
看起来像真相库 (Maven 存储库)。

根据 Android 文档和建议,Truth 是首选库。它是 Jetpack 文档的一部分。

Jetpack 是一套库,可帮助开发人员遵循最佳实践、减少样板代码并编写跨 Android 版本和设备一致运行的代码,以便开发人员可以专注于他们关心的代码。

请参阅使用正确导入的测试示例Git
有关文档assertThat(Integer)

AndroidX 是 Truth 的扩展

不属于的其他扩展
真相项目本身包括:

  • AndroidX Test,用于测试 Android 类型,例如 Intent

使用 AndroidX 设置项目

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

Truth 与 AssertJ

Truth 和 AssertJ 非常相似。这就提出了一个问题:为什么
我们创造真理?原因是历史性的:AssertJ 不存在的时候
我们开始了真理。当它创建时,我们已经开始迁移
谷歌编码为真相,我们做出了一些设计决策
很难改造到 AssertJ 上。

This is from AssertJ library.

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);

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

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"

Truth vs. 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.

贱人配狗天长地久 2025-01-27 08:45:36

使用“断言”而不是“断言”。

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
}

Use “assert” than “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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文