gpt4 book ai didi

list - AssertJ:containsOnly和containsExactlyInAnyOrder有什么区别

转载 作者:行者123 更新时间:2023-12-03 16:03:36 25 4
gpt4 key购买 nike

AbstractIterableAssert#containsOnly说:

Verifies that the actual group contains only the given values and nothing else, in any order.



AbstractIterableAssert#containsExactlyInAnyOrder说:

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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com