Map equality using Hamcrest

后端 未结 8 2139
鱼传尺愫
鱼传尺愫 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:09

    Another option available now is to use the Cirneco extension for Hamcrest. It has hasSameKeySet() (as well as other matchers for Guava "collections"). According to your example, it will be:

    assertThat(affA, hasSameKeySet(affB));
    

    You can use the following dependency for a JDK7-based project:

    
      it.ozimov
      java7-hamcrest-matchers
      0.7.0
    
    

    or the following if you are using JDK8 or superior:

    
      it.ozimov
      java8-hamcrest-matchers
      0.7.0
    
    

提交回复
热议问题