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

标签 list unit-testing junit assert assertj

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/

    相关文章:

    Python - 合并列表中的一行列表

    unit-testing - Android Studio,assembleDebugTest导致多个dex文件定义

    java - 将第三方对象传递给构造函数的最佳方法是什么

    java - 有一个用于用户注册的 junit 测试用例有意义吗?

    python - 是否有用于创建大小为 'n' 的随机数列表的内置函数?

    ios - IOS 应用程序的下拉菜单

    java - 尝试从 ArrayList 中为每个用户打印出来

    c# - 更改生成的单元测试文件的目录

    angularjs - Angular 单元测试中对其他服务的额外依赖

    java - 如何验证方法内部正在启动的方法?