的實際差異事情只有當預期與實際的集合/列表包含重複:
雖然,他們都失敗,如果:
至少一個預期唯一值是缺少實際的集合
並非每一個獨特來自實際收集的值被斷言
見例如:
private List<String> withDuplicates;
private List<String> noDuplicates;
@Before
public void setUp() throws Exception {
withDuplicates = asList("Entryway", "Underhalls", "The Gauntlet", "Underhalls", "Entryway");
noDuplicates = asList("Entryway", "Underhalls", "The Gauntlet");
}
@Test
public void exactMatches_SUCCESS() throws Exception {
// successes because these 4 cases are exact matches (bored cases)
assertThat(withDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 1
assertThat(withDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 2
assertThat(noDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls"); // 3
assertThat(noDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls"); // 4
}
@Test
public void duplicatesAreIgnored_SUCCESS() throws Exception {
// successes because actual withDuplicates contains only 3 UNIQUE values
assertThat(withDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls"); // 5
// successes because actual noDuplicates contains ANY of 5 expected values
assertThat(noDuplicates).containsOnly("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 6
}
@Test
public void duplicatesCauseFailure_FAIL() throws Exception {
SoftAssertions.assertSoftly(softly -> {
// fails because ["Underhalls", "Entryway"] are UNEXPECTED in actual withDuplicates collection
softly.assertThat(withDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls"); // 7
// fails because ["Entryway", "Underhalls"] are MISSING in actual noDuplicates collection
softly.assertThat(noDuplicates).containsExactlyInAnyOrder("Entryway", "The Gauntlet", "Underhalls", "Entryway", "Underhalls"); // 8
});
}
我正要澄清,'containsOnly'的javadoc,一個更有理由這樣做。 –
完成https://github.com/joel-costigliola/assertj-core/blob/6983158e5ea2f6fe54c864fc0dd7ec9b672e4653/src/main/java/org/assertj/core/api/ObjectEnumerableAssert.java#L61 –
感謝您的精彩圖書館和快速支持! – radistao