【问题标题】:Cannot import assertThat method junit无法导入assertThat方法junit
【发布时间】:2018-07-10 07:38:13
【问题描述】:

我正在使用 hamcrest 和 junit 执行测试,我需要比较 2 个具有相同项目但顺序不同的地图,因此 assertEquals 对我不起作用。 我已经看到了已经给出的答案,但是我无法导入所需的方法。

我是通过以下方式导入

import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;

然后我尝试在答案中看到的下一件事

assertThat(expectedMap, containsInAnyOrder(receivedMap));

但不识别方法 assertThat,IDE 向我显示一个警告,要求我创建方法

【问题讨论】:

    标签: java unit-testing hamcrest junit5


    【解决方案1】:

    Map 没有实现Iterable。因此,方法签名与推断的参数类型 Map<?>Matcher<Iterable<?>> 不匹配。

    【讨论】:

      【解决方案2】:

      您导入了错误的assertThat 方法,您想要的是来自hamcrest 的方法:

      import static org.hamcrest.MatcherAssert.assertThat;
      

      【讨论】:

      • 我改了,还是不行,警告还在继续
      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2021-01-22
      • 2019-04-08
      相关资源
      最近更新 更多