简体   繁体   English

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

[英]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'无法解析符号“currentDestination”

After some digging, I find getCurrentDestination() .经过一番挖掘,我找到了getCurrentDestination() I also have to change id to getId() to fix a similar error.我还必须将id更改为getId()以修复类似的错误。

Then I get然后我得到

Cannot resolve method 'assertThat(int)'无法解析方法“assertThat(int)”

What version of assertThat() should I import?我应该导入哪个版本的assertThat() I found 2 versions in JUnit , but neither takes only one parameter.在 JUnit 中找到了2 个版本,但都只接受一个参数。 Besides both are deprecated.此外,两者都已弃用。 org.hamcrest.MatcherAssert has 3 versions of assertThat() , but again, none take a single int or Integer parameter. org.hamcrest.MatcherAssert有 3 个版本的assertThat() ,但同样,没有一个采用单个intInteger参数。 So where do I find the right version of assertThat() ?那么我在哪里可以找到正确版本的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?除此之外,我似乎需要所有这些更改来修复官方 android 文档中的示例,我在这里缺少什么? Or is this example broken?或者这个例子坏了?

This is from AssertJ library.这来自AssertJ库。
Maven : 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 AssertJ 是一个 Java 库,它提供了丰富的断言集和真正有用的错误消息,提高了测试代码的可读性,并且被设计为超级易用

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

See documentation assertThat(int)请参阅文档assertThat(int)

Update:更新:
I have reviewed more deeply android docs.我更深入地审查了 android 文档。 Looks like Truth library ( Maven repo ) is used.看起来像使用了Truth 库( Maven repo )。
Truth is the preferred library according to android docs and recommendations.根据 android 文档和建议,Truth 是首选库。 It is part of Jetpack documentation .它是 Jetpack 文档的一部分。
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. Jetpack 是一套库,可帮助开发人员遵循最佳实践、减少样板代码并编写可跨 Android 版本和设备一致工作的代码,以便开发人员可以专注于他们关心的代码。


See example of test with correct imports.请参阅正确导入的测试示例 Git Git
Documentation about assertThat(Integer)关于assertThat(Integer)的文档

AndroidX is an extension of Truth AndroidX是Truth的延伸

Other extensions that are not part of the Truth project itself include:不属于 Truth 项目本身的其他扩展包括:

  • AndroidX Test for testing Android types, like Intent用于测试 Android 类型的 AndroidX 测试,例如 Intent

Setup project with AndroidX 使用 AndroidX 设置项目

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

Truth vs. AssertJ真相与 AssertJ

Truth and AssertJ are very similar. Truth 和 AssertJ 非常相似。 This raises the question: Why did we create Truth?这就提出了一个问题:我们为什么要创造真理? The reason is historical: AssertJ didn't exist when we started Truth.原因是历史性的:当我们启动 Truth 时,AssertJ 还不存在。 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.在创建它时,我们已经开始将 Google 代码迁移到 Truth,并且我们做出了一些难以 retrofit 到 AssertJ 的设计决策。

Use “assert” than “assertThat”.使用“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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM