Map equality using Hamcrest

后端 未结 8 2008
鱼传尺愫
鱼传尺愫 2021-02-06 20:25

I\'d like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values.

My current best guess is:

a         


        
8条回答
  •  太阳男子
    2021-02-06 21:11

    If you need to compare a set of results with expectations and if you choose to use assertj library, you can do this:

    // put set of expected values by your test keys
    Map expectations = ...;
    
    // for each test key get result
    Map results = expectations.keySet().stream().collect(toMap(k -> k, k -> getYourProductionResult(k)));
    
    assertThat(results).containsAllEntriesOf(expectations);
    

    Note that containsAllEntriesOf does not compare maps for equality. If your production code returns actually a Map you may want to add a check for keys assertThat(results).containsOnlyKeys((K[]) expectations.keySet().toArray());

提交回复
热议问题