【发布时间】:2014-11-19 18:35:14
【问题描述】:
我正在写一些 junit,并进行检查,比较两个 hashmap 的键和值
Iterator<Map.Entry<String, String>> it = expected.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
assertTrue("Checks key exists", actual.containsKey(pairs.getKey()));
assertThat("Checks value", actual.get(pairs.getKey()), equalTo(pairs.getValue()));
}
效果很好,但我有一个价值会绊倒它:
java.lang.AssertionError: Checks value
Expected: "Member???s "
but: was "Member���s "
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
我检查了数据,数据是正确的。看来三联?不知何故绊倒了一些东西。有谁知道为什么会被绊倒?这对我来说似乎很基本,它甚至没有弄乱 hamcrest,它是真正的断言。
【问题讨论】:
-
数据从何而来?从一个文件?检查编码。
-
它来自 Oracle 数据库。它映射到字符串的事实应该使它们相等,不是吗?
-
将加载的值打印到控制台或写入日志。一定是编码问题。我认为 JUnit/Hamcrest 不是问题。
-
旁注:你不需要
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();中的演员表。 -
那些看起来像不可打印的字符而不是问号。尝试打印它们的 Unicode 值。