JUnit测试在运行模式下通过,但在调试模式下失败



简介

当我在运行模式下运行此测试时

@Test
public void testGetFinalOutput() {
final ObjectLocator dut = new ObjectLocator(ImmutableMap.of(
TEST_ITEM, new Coordinates(1,1)));
Assert.assertTrue(dut.getFinalOutput(1, 1).isPresent());
}

这将运行并传递断言。

如果我在调试模式下运行相同的测试,我会得到以下错误和堆栈:

java.lang.ExceptionInInitializerError
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Multiple entries with same key: (2, 0)=[GameObject{blocking=true, grabbable=false, sprite=ObjectGeometry{foreColor=java.awt.Color[r=255,g=255,b=255], backColor=java.awt.Color[r=0,g=0,b=0], imgChar=AMPERSAND}, name=test wall, desires=[], detailedDescription=This is a test wall, uuid=6e84f9a5-5039-40a2-acc1-793d50ebded4}] and (2, 0)=[GameObject{blocking=true, grabbable=false, sprite=ObjectGeometry{foreColor=java.awt.Color[r=255,g=255,b=255], backColor=java.awt.Color[r=0,g=0,b=0], imgChar=AMPERSAND}, name=test wall, desires=[], detailedDescription=This is a test wall, uuid=32ad0809-4252-4883-896a-a3b228c9c250}]
at com.google.common.collect.ImmutableMap.conflictException(ImmutableMap.java:211)
at com.google.common.collect.ImmutableMap.checkNoConflict(ImmutableMap.java:205)
at com.google.common.collect.RegularImmutableMap.checkNoConflictInKeyBucket(RegularImmutableMap.java:146)
at com.google.common.collect.RegularImmutableMap.fromEntryArray(RegularImmutableMap.java:109)
at com.google.common.collect.ImmutableMap$Builder.build(ImmutableMap.java:390)
at com.google.common.collect.ImmutableSetMultimap.fromMapEntries(ImmutableSetMultimap.java:420)
at com.google.common.collect.ImmutableSetMultimap.copyOf(ImmutableSetMultimap.java:381)
at com.google.common.collect.ImmutableSetMultimap.copyOf(ImmutableSetMultimap.java:363)
at location.ObjectLocator.mergeValues(ObjectLocator.java:147)
at location.ObjectLocator.union(ObjectLocator.java:165)
at location.ObjectLocator.union(ObjectLocator.java:60)
at generation.BuildingUtils.runWalls(BuildingUtils.java:125)
at generation.BuildingUtils.buildClosedPolygon(BuildingUtils.java:139)
at generation.BuildingUtils.buildRectangle(BuildingUtils.java:67)
at location.ObjectLocatorTest.<clinit>(ObjectLocatorTest.java:39)
... 46 more

详细信息

ObjectLocatorTest.java中触发此操作的第39行是静态字段初始化的一部分:

private static final ObjectLocator VERTICAL = BuildingUtils.buildRectangle(
TEST_WALL_FACTORY, TEST_FLOOR_FACTORY,
3,5).translate(1,0);

这是我在ObjectLocator.java:中的mergeValues

private static <K,V> ImmutableMultimap<K,V> mergeValues(final Multimap<K, V> mapA, final Multimap<K,V> mapB) {
final Multimap<K,V> retVal = HashMultimap.create();
for(Map.Entry<K, V> entry : mapA.entries()) {
retVal.put(entry.getKey(), entry.getValue());
}
for(Map.Entry<K, V> entry : mapB.entries()) {
retVal.put(entry.getKey(), entry.getValue());
}
return ImmutableSetMultimap.copyOf(retVal);
}

其中return ImmutableSetMultimap.copyOf(retVal);在线路147上。难道Multimap不应该能够处理";具有相同关键字的多个条目";,就像stacktrace抱怨的那样?

版本

  • 我正在IntelliJ IDEA Ultimate 2020.2中运行此程序
  • 我用的是番石榴30.1-jre
  • 我正在使用Java 8

在键类中,您覆盖了Object的equals(Object obj),但没有覆盖hashCode()

正确覆盖两者。

最新更新