CollectionAssert in jUnit?
有没有与NUnit的CollectionAssert平行的jUnit
?
有没有与NUnit的CollectionAssert平行的jUnit
?
使用JUnit 4.4,您可以与Hamcrest代码一起使用(不用担心,它与JUnit一起提供,不需要额外的)来生成复杂的自描述断言,包括对集合进行操作的断言:assertThat()
.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"))));
使用此方法,您将在断言失败时自动获得断言的良好描述。