AssertJ:是什么containsOnly和containsExactlyInAnyOrder

AbstractIterableAssert#containsOnly之间的区别说:AssertJ:是什么containsOnly和containsExactlyInAnyOrder

验证实际组只包含给定值,没有别的,以任何顺序。

AbstractIterableAssert#containsExactlyInAnyOrder说:

验证实际组包含以任何顺序一个与给定值,没有别的,。

的描述看起来几乎一样,所以是什么只有正是之间的实际差异?

回答:

的实际差异事情只有当预期与实际的集合/列表包含重复:

  • 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

});

}

以上是 AssertJ:是什么containsOnly和containsExactlyInAnyOrder 的全部内容, 来源链接: utcz.com/qa/265007.html

回到顶部