Java jUnit 中的 CollectionAssert?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1086691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:28:04  来源:igfitidea点击:

CollectionAssert in jUnit?

javajunitnunitassertion

提问by ripper234

Is there a jUnit parallel to NUnit's CollectionAssert?

是否有与 NUnit 并行的 jUnit CollectionAssert

采纳答案by Joachim Sauer

Using JUnit 4.4 you can use assertThat()together with the Hamcrestcode (don't worry, it's shipped with JUnit, no need for an extra .jar) to produce complex self-describing asserts including ones that operate on collections:

使用 JUnit 4.4,您可以assertThat()Hamcrest代码(别担心,它随 JUnit 一起提供,不需要额外的.jar)来生成复杂的自描述断言,包括对集合进行操作的断言:

import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.*;
import static org.hamcrest.CoreMatchers.*;

List<String> l = Arrays.asList("foo", "bar");
assertThat(l, hasItems("foo", "bar"));
assertThat(l, not(hasItem((String) null)));
assertThat(l, not(hasItems("bar", "quux")));
// check if two objects are equal with assertThat()

// the following three lines of code check the same thing.
// the first one is the "traditional" approach,
// the second one is the succinct version and the third one the verbose one 
assertEquals(l, Arrays.asList("foo", "bar")));
assertThat(l, is(Arrays.asList("foo", "bar")));
assertThat(l, is(equalTo(Arrays.asList("foo", "bar"))));

Using this approach you will automagically get a good description of the assert when it fails.

使用这种方法,您将在断言失败时自动获得对断言的良好描述。

回答by skaffman

Not directly, no. I suggest the use of Hamcrest, which provides a rich set of matching rules which integrates nicely with jUnit (and other testing frameworks)

不直接,不。我建议使用Hamcrest,它提供了一组丰富的匹配规则,可以很好地与 jUnit(和其他测试框架)集成

回答by Tomek Kaczanowski

Take a look at FEST Fluent Assertions. IMHO they are more convenient to use than Hamcrest (and equally powerful, extensible etc) and have better IDE support thanks to fluent interface. See https://github.com/alexruiz/fest-assert-2.x/wiki/Using-fest-assertions

看看 FEST Fluent Assertions。恕我直言,它们比 Hamcrest 更方便使用(并且同样强大,可扩展等)并且由于流畅的界面具有更好的 IDE 支持。见https://github.com/alexruiz/fest-assert-2.x/wiki/Using-fest-assertions

回答by gavs

Joachim Sauer's solution is nice but doesn't work if you already have an array of expectations that you want to verify are in your result. This might come up when you already have a generated or constant expectation in your tests that you want to compare a result to, or perhaps you have multiple expectations you expect to be merged in the result. So instead of using matchers you can can just use List::containsAlland assertTrueFor Example:

Joachim Sauer 的解决方案很好,但如果您已经有一系列要验证的期望值在您的结果中,则该解决方案不起作用。当您的测试中已经有一个生成的或恒定的期望与结果进行比较时,可能会出现这种情况,或者您可能有多个期望合并到结果中。因此,您可以使用List::containsAllandassertTrue例如,而不是使用匹配器:

@Test
public void testMerge() {
    final List<String> expected1 = ImmutableList.of("a", "b", "c");
    final List<String> expected2 = ImmutableList.of("x", "y", "z");
    final List<String> result = someMethodToTest(); 

    assertThat(result, hasItems(expected1)); // COMPILE ERROR; DOES NOT WORK
    assertThat(result, hasItems(expected2)); // COMPILE ERROR; DOES NOT WORK

    assertTrue(result.containsAll(expected1));  // works~ but has less fancy
    assertTrue(result.containsAll(expected2));  // works~ but has less fancy
}