Map equality using Hamcrest

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

    I favor using Guava ImmutableMap. They support Map.equals() and are easy to construct. The only trick is to explicitly specify type parameters, since hamcrest will assume the ImmutableMap type.

    assertThat( actualValue,
                Matchers.>equalTo( ImmutableMap.of(
                    "key1", "value",
                    "key2", "other-value"
    ) ) );
    

提交回复
热议问题