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

时间:2021-03-12 04:21:02

标签: java gradle intellij-idea junit guava

简介

当我在运行模式下运行这个测试

@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);

这是我在 mergeValues 中的 ObjectLocator.java

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

1 个答案:

答案 0 :(得分:0)

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

正确覆盖两者。