【发布时间】:2013-08-10 19:37:55
【问题描述】:
我正在通过组合其他三个哈希图( )并添加文件名来构建一个新的哈希图( )。如何断言新的 hashmap 是正确的?嵌套数组使测试失败。
此代码是我的失败测试的简化示例:
@Test
public void injectArrayIntoHashMap() {
HashMap map = new HashMap();
map.put("hi", new String[] { "hello", "howdy" });
HashMap newMap = new HashMap();
newMap.put("hi", new String[] { "hello", "howdy" });
assertEquals(map, newMap);
}
更新:好的,根据 Hna 的建议,我使用 ArrayList 进行了测试。但是,我随后意识到我需要在 ArrayList 中实例化一个对象,现在测试失败了。这似乎与 ArrayList 中的对象具有不同的内存地址这一事实有关。我是 Java 新手,将对象插入到 ArrayList 中,这是我避免使用“if”语句的尝试。有没有更好的办法?或者只是让我的测试通过的简单答案?
这是新代码:
@Test
public void sampleTest() throws IOException {
HashMap expectedResult = new HashMap();
expectedResult.put("/images", new ArrayList(Arrays.asList("/images", new Public())));
expectedResult.put("/stylesheets", new ArrayList(Arrays.asList("/stylesheets", new Public())));
HashMap actualResult = test();
assertEquals(expectedResult, actualResult);
}
public HashMap test() {
HashMap hashMap = new HashMap();
hashMap.put("/images", new ArrayList(Arrays.asList("/images", new Public())));
hashMap.put("/stylesheets", new ArrayList(Arrays.asList("/stylesheets", new Public())));
return hashMap;
}
【问题讨论】:
标签: java arrays junit hashmap multidimensional-array