我需要编写 Matcher 来检查多个属性。对于我使用过的单个属性:
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
Matcher<Class> matcherName = Matchers.<Class> hasProperty("propertyName",equalTo(expectedValue));
我应该如何检查一个 Matcher 中的更多属性?
最佳答案
您可以通过将匹配器与 allOf
结合使用一个匹配器来检查更多属性:
Matcher<Class> matcherName = allOf(
hasProperty("propertyName", equalTo(expected1)),
hasProperty("propertyName2", equalTo(expected2)));
但我猜你真正要找的是 samePropertyValuesAs
一个,它通过检查属性本身而不是 equals 来检查一个 bean 是否与另一个 bean 具有相同的属性值
方法:
assertThat(yourBean, samePropertyValuesAs(expectedBean));
关于java - 在一个匹配器中匹配多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35437038/