JUnit 4 compare Sets

后端 未结 9 1195
-上瘾入骨i
-上瘾入骨i 2020-12-24 03:59

How would you succinctly assert the equality of Collection elements, specifically a Set in JUnit 4?

9条回答
  •  猫巷女王i
    2020-12-24 04:41

    Using Hamcrest:

    assertThat( set1, both(everyItem(isIn(set2))).and(containsInAnyOrder(set1)));
    

    This works also when the sets have different datatypes, and reports on the difference instead of just failing.

提交回复
热议问题