java - 在一个匹配器中匹配多个属性

标签 java mockito matcher hamcrest

我需要编写 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/

相关文章:

java - PowerMockito 在父类(super class)静态方法上给出 InvalidUseOfMatchersException

java - 在 Java 中,如何使用模式和匹配器将大字符串拆分为特定的子字符串?

java - 如何在 Windows 中将 jar 添加到 jconsole 类路径?

java - 当分割字符恰好出现两次时分割字符串

java mockito 无法创建类型

java - 如何使用匹配器在 TextView 中隐藏#

javascript - Jest : expect object not to have property

java - Spark 因 SerializedLambda 的 ClassNotFoundException 失败

c# - 将文件复制到 .jar 文件会导致错误

java - 尽管 url 有效,Spring Controller 测试仍失败