在hamcrest中包含对面

pih*_*agy 12 java hamcrest

包含的相反之处是什么?

    List<String> list = Arrays.asList("b", "a", "c");
    // should fail, because "d" is not in the list

    expectedInList = new String[]{"a","b", "c", "d"};
    Assert.assertThat(list, Matchers.contains(expectedInList));


    // should fail, because a IS in the list
    shouldNotBeInList = Arrays.asList("a","e", "f", "d");
    Assert.assertThat(list, _does_not_contains_any_of_(shouldNotBeInList)));
Run Code Online (Sandbox Code Playgroud)

应该是_does_not_contains_any_of_什么?

eee*_*eee 12

您可以通过以下方式组合三个内置匹配器:

import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.not;

@Test
public void hamcrestTest() throws Exception {
    List<String> list = Arrays.asList("b", "a", "c");
    List<String> shouldNotBeInList = Arrays.asList("a", "e", "f", "d");
    Assert.assertThat(list, everyItem(not(isIn(shouldNotBeInList))));
}
Run Code Online (Sandbox Code Playgroud)

执行此测试将为您提供:

预期:每个项目都不是{"a","e","f","d"}之一
但是:项目是"a"

  • 在“hasItems()”方法之后,这是非常违反直觉的。我尝试使用“not(hasItems())”一段时间但没有成功。感谢您的回答。 (2认同)