【问题标题】:Use Hamcrest's assertThat with several objects in one line?将 Hamcrest 的 assertThat 与一行中的多个对象一起使用?
【发布时间】:2014-11-21 03:28:06
【问题描述】:

我可以使用 Hamcrest 的 assertThat 在一行代码中断言有关多个对象的内容吗?对象不在集合中。比如我想assertThat四个字符串是相等的。另外,如何断言所有对象都是空的?

【问题讨论】:

标签: junit hamcrest


【解决方案1】:

使用 Hamcrest 的 Every

assertThat(listOfStrings, Every.everyItem(CoreMatchers.equalTo("someValue"));
assertThat(listOfStrings, Every.everyItem(CoreMatchers.nullValue());

【讨论】:

    【解决方案2】:
    assertThat("All values should be equals to origin", origin, allOf(is(str1), is(str2));
    assertThat("All values should be null", null, allOf(is(obj1), is(obj2));
    

    【讨论】:

    • 我认为,当您为您的意图添加一些解释时,这对操作人员和其他访问者会更有帮助。
    • @reporter 请注意,此回答者 OP(Q&A 风格)。但你的观点仍然有效。
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2016-05-24
    相关资源
    最近更新 更多