简体   繁体   English

忽略 Map 中的字段<string, object>在测试时是 class 的成员</string,>

[英]Ignoring a field in Map<String, Object> that is a member of a class while testing

I have an Object called ReconciliationResult我有一个名为 ReconciliationResult 的 Object

public class ReconciliationResult {

  Map<String, Object> recordValue;
  List<Object> keyValues;
  Origin origin;
  ReconciliationResultStatus status;

  public enum ReconciliationResultStatus {
    INVALID_KEY,
    DUPLICATE_KEY,
    MATCHING,
    NON_MATCHING;
  }

  public enum Origin {
    LEFT_SIDE,
    RIGHT_SIDE;
  }
}

I am comparing an instance of this object to the result of my classUnder test我正在将此 object 的实例与我的 classUnder 测试的结果进行比较

EDIT:编辑:

    List<ReconciliationResult> results =
        reconciliationRegistry.getRecon("BPSVsGPM_TradeDated").reconcile(TESTDATE);
    assertThat(results).usingRecursiveComparison().ignoringFields("id").isEqualTo(expectedMatch);

However I don't want to test the ID field in the recordValue field inside of my ReconciliationResult Object.但是,我不想测试 ReconciliationResult Object 内的 recordValue 字段中的 ID 字段。 I don't want to test it because I have multiple tests in this class and every time I insert something to my embedded PG db the ID increments so on assertions, ID is never the same.我不想测试它,因为我在这个 class 中有多个测试,并且每次我在嵌入式 PG db 中插入一些东西时,ID 都会增加,所以断言,ID 永远不会相同。

I have tried clearing the database after every run using JdbcTemplate , but that didn't work I also added the @DirtiesContext annotation since the tests are transactional.我曾尝试在每次运行后使用JdbcTemplate清除数据库,但这不起作用我还添加了@DirtiesContext注释,因为测试是事务性的。 Alas those approaches didn't work as well.唉,这些方法也没有奏效。

Any clarification on the matter would be greatly appreciated.对此事的任何澄清将不胜感激。 Please let me know if you need any more information from me.如果您需要我提供更多信息,请告诉我。

Are you not invoking the methods in incorrect sequence here?您不是在这里以错误的顺序调用方法吗? assertThat(results).isEqualTo(expectedMatch).usingRecursiveComparison().ignoringFields("id");

You probably need to invoke comparison and ignore the fields before equals.您可能需要调用比较并忽略等于之前的字段。

assertThat(results).usingRecursiveComparison().ignoringFields("id").isEqualTo(expectedMatch);

I had to do the comparison of the map separately since the ignoringFields is for object members, and can't go into the map specifically to ignore the key. I had to do the comparison of the map separately since the ignoringFields is for object members, and can't go into the map specifically to ignore the key. I created a helper function to filter out the ID field in my Map<String, Object> field during comparison.我创建了一个助手 function 在比较期间过滤掉我的 Map<String, Object> 字段中的 ID 字段。

    assertThat(results)
        .usingRecursiveComparison()
        .ignoringFields("recordValue", "keyValues")
        .isEqualTo(expectedBreak);
    assertThat(filterIgnoredKeys(results.get(0).getRecordValue()))
        .usingRecursiveComparison()
        .isEqualTo(filterIgnoredKeys(expectedBreak.get(0).getRecordValue()));


  private static <V> Map<String, V> filterIgnoredKeys(Map<String, V> map) {
    return Maps.filterKeys(map, key -> !IGNORED_FIELDS.contains(key));
  }

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

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