繁体   English   中英

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 行是 static 字段初始化的一部分:

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

在您的密钥 class 中,您覆盖了对象的equals(Object obj)但不是hashCode()

正确覆盖两者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM