【问题标题】:How to assert that a list does not contain something, with FEST?如何使用 FEST 断言列表不包含某些内容?
【发布时间】:2014-05-15 14:37:06
【问题描述】:

我正在使用FEST 在 JUnit 中编写断言。

很容易断言列表包含一些元素:

assertThat(list).contains(2,4);

但是如何断言列表不包含某些内容?喜欢:

assertThat(list).doesnotContain(3); // !!! no such method

【问题讨论】:

    标签: java testing fest


    【解决方案1】:

    我刚刚浏览了版本 1 分支的源代码,发现了这个:

    /**
     * Verifies that the actual group of objects does not contain the given objects.
     *
     * @param objects the objects that the group of objects should exclude.
     * @return this assertion object.
     * @throws AssertionError       if the actual group of objects is {@code null}.
     * @throws NullPointerException if the given array is {@code null}.
     * @throws AssertionError       if the actual group of objects contains any of the given objects.
     */
    public final @Nonnull S excludes(@Nonnull Object... objects) {
        assertExcludes(objects);
        return myself();
    }
    

    我可能不应该做出这样的疯狂假设,但它与您的 contains 方法 (ObjectGroupAssert) 属于同一类,并且 Javadoc 似乎描述了您正在寻找的功能。

    所以我想,你只需要:

    assertThat(list).excludes(5,7);
    

    【讨论】:

    • 有效!方法名excludes让我有点意外
    • 嘘! :) 是的,excludes 对我来说也是新的,我期待一个 doesNotContain 变体。但仔细想想,我喜欢排除,但它可能更适合包含,因为它是对应部分而不是包含。
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多