作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
AbstractIterableAssert#containsOnly说:
Verifies that the actual group contains only the given values and nothing else, in any order.
Verifies that the actual group contains exactly the given values and nothing else, in any order.
最佳答案
仅当预期和实际的集合/列表包含重复的时,实际的差异才有意义:
containsOnly
始终与敏感区重复:如果期望/实际值集与containsExactlyInAnyOrder
始终重复敏感:如果期望/实际元素的数量不同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
});
}
关于list - AssertJ:containsOnly和containsExactlyInAnyOrder有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47992139/
我的自定义对象如下所示: @Accessors(chain = true) @Getter @Setter @AllArgsConstructor @ToString public class Soc
我是一名优秀的程序员,十分优秀!