爲什麼此測試失敗?我知道包含的作品,當你傳遞以逗號分隔的單獨的字符串,但我想看看是否有可能傳遞整個字符串列表。我只是想確保列表1包含列表2的所有內容。如何使用hamcrest包含比較2列表?
@Test
public void testContains() {
String expected1 = "hello";
String expected2 = "goodbye";
List<String> expectedStrings = new ArrayList<>();
expectedStrings.add(expected1);
expectedStrings.add(expected2);
List<String> actualStrings = new ArrayList<>();
actualStrings.add(expected1);
actualStrings.add(expected2);
assertThat(actualStrings, contains(expectedStrings));
}
是否認爲可以使用此斷言來代替?
assertThat(actualStrings, is(expectedStrings));
containsAll而不是contains。檢查[API](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#containsAll-java.util.Collection-) –
@RahulSharma,所以你可以像使用' assertThat(actualStrings.containsAll(expectedStrings),(true))'? –
@RahulSharma我正在尋找更多的是否有類似於List方法'containsAll'的hamcrest匹配器,所以我可以這樣做:'assertThat(actualStrings,containsAll(expectedStrings))''。我認爲它比「匹配(匹配)」更好。 –