有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我该如何在一个匹配器中检查更多属性


共 (1) 个答案

  1. # 1 楼答案

    通过将匹配器与allOf组合,可以使用一个匹配器检查更多属性:

        Matcher<Class> matcherName = allOf(
            hasProperty("propertyName", equalTo(expected1)),
            hasProperty("propertyName2", equalTo(expected2)));
    

    但我猜你实际上在寻找的是samePropertyValuesAs方法,它通过检查属性本身而不是equals方法来检查一个bean是否与另一个bean具有相同的属性值:

        assertThat(yourBean, samePropertyValuesAs(expectedBean));