JUnit 测试在 运行 模式下通过,但在调试模式下失败?
JUnit test passes in Run mode, but fails in debug mode?
简介
当我运行这个测试在运行模式
@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 不能像堆栈跟踪所抱怨的那样处理“具有相同键的多个条目”吗?
版本
- 我正在 运行在 IntelliJ IDEA Ultimate 2020.2 中使用它。
- 我正在使用番石榴 30.1-jre
- 我正在使用 Java 8
在您的密钥 class 中,您覆盖了对象的 equals(Object obj)
但未覆盖 hashCode()
。
正确覆盖两者。
简介
当我运行这个测试在运行模式
@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 不能像堆栈跟踪所抱怨的那样处理“具有相同键的多个条目”吗?
版本
- 我正在 运行在 IntelliJ IDEA Ultimate 2020.2 中使用它。
- 我正在使用番石榴 30.1-jre
- 我正在使用 Java 8
在您的密钥 class 中,您覆盖了对象的 equals(Object obj)
但未覆盖 hashCode()
。
正确覆盖两者。