使用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 pre 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"))));
使用此方法,您将在断言失败时自动获得对断言的良好描述。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)