【问题标题】:The method is(int) is undefined方法 is(int) 未定义
【发布时间】:2019-06-21 21:51:43
【问题描述】:

我使用以下导入:

import static org.junit.Assert.*;

但是在下面一行:

assertThat(0, is(1));

我收到The method is(int) is undefined for the type AppTest

这个方法的名字在英文里太常见了,我找不到在哪里导入这个。

所以我的问题是,我可以在哪里导入 is(和 not)。

【问题讨论】:

  • 你为什么不用assertEquals()
  • @shmosel 因为 assertThat(expected,someMatcher()) 通常更具可读性,并且更容易根据用例进行调整,从而产生更好的错误消息。说真的:几乎所有时候,我发现 assertThat 你唯一需要的断言。
  • 感谢您的快速接受,我断言它现在对您有用 ;-)
  • 你可能错了

标签: java junit assertthat


【解决方案1】:

你需要

import static org.hamcrest.Matchers.is;

is()等很多大帮手都是hamcrestMatchers。当然,您需要以某种方式定义相应的依赖项。它可能会有点棘手,因为 hamcrest 有一些核心匹配器,并且匹配器类具有“所有”匹配器。但是,一旦您确定了哪一个最适合您,您就已经准备好了,一切都很好。

【讨论】:

    【解决方案2】:

    org.assertj.core.api.Assertions 广泛用于测试用例....

    import static org.assertj.core.api.Assertions.*;
    
    assertThat(actual).isEqualTo(expected);
    assertThat(actual).isNotEqualTo(expected);
    

    【讨论】:

    • 在这里投赞成票,尽管这确实回答了这个问题。请注意:它引入了另一个依赖项,因此您至少应该提到需要在项目设置中“包含”assertj。也不是那个 assertj assertThat 不是junit assertThat + hamcrest 匹配器!
    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多