Hamcrest 1.3 使用稍微不同的语法,一些例子:
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
...
@Test
public void inOrder1() {
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")));
}
@Test(expected = AssertionError.class)
public void inOrder2() {
assertThat(new String[]{"bar", "foo"}, arrayContaining(equalTo("foo"), equalTo("bar")));
// Expected: ["foo", "bar"]
// but: item 0: was "bar"
}
@Test(expected = AssertionError.class)
public void inOrder3() {
assertThat(new String[]{"foo", "bar", "lux"}, arrayContaining(equalTo("foo"), equalTo("bar")));
// Expected: ["foo", "bar"] in any order
// but: Not matched: "lux"
}
@Test(expected = AssertionError.class)
public void inOrder4() {
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar"), equalTo("lux")));
// Expected: ["foo", "bar", "lux"] in any order
// but: No item matched: "lux" in ["foo", "bar"]
}