使用
Every匹配器。
import org.hamcrest.beans.HasPropertyWithValue;import org.hamcrest.core.Every;import org.hamcrest.core.Is;import org.junit.Assert;Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty("gender", Is.is("male")))));
Hamcrest还提供
Matchers#everyItem了一种快捷方式
Matcher。
完整的例子
@org.junit.Testpublic void method() throws Exception { Iterable<Person> people = Arrays.asList(new Person(), new Person()); Assert.assertThat(people, (Every.everyItem(HasPropertyWithValue.hasProperty("gender", Is.is("male")))));}public static class Person { String gender = "male"; public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)