如何比较Java中预期对象属性为动态的两个对象

如何比较Java中预期对象属性为动态的两个对象,java,compare,cucumber,comparator,gherkin,Java,Compare,Cucumber,Comparator,Gherkin,我正在寻找一种基于Cucumber/Gherkin格式编写的动态属性来比较Java中对象的方法 有没有人实现过类似的东西,或者知道可以实现这一点的框架 下面是我尝试做的一个例子: 黄瓜特征 StepDefinitions.java 注意-Assert.assertEquals()显然不起作用 有什么想法吗 谢谢 Ben:)覆盖响应对象中的等于方法。在重写方法中实现比较逻辑 编辑 正如@wietrol所注意到的,重写hashcode方法也是如此。关于这方面的更多信息,您可以在此处阅读,还请确保覆盖

我正在寻找一种基于Cucumber/Gherkin格式编写的动态属性来比较Java中对象的方法

有没有人实现过类似的东西,或者知道可以实现这一点的框架

下面是我尝试做的一个例子:

黄瓜特征

StepDefinitions.java

注意-Assert.assertEquals()显然不起作用

有什么想法吗

谢谢


Ben:)

覆盖
响应对象中的
等于
方法。在重写方法中实现比较逻辑

编辑


正如@wietrol所注意到的,重写
hashcode
方法也是如此。关于这方面的更多信息,您可以在此处阅读

,还请确保覆盖hashCode()方法。。。要使equals()接受对象,使用字典而不是泛型属性可能是一个不错的选择。我给您留下一个反射比较器示例(例如org.apache.commons.lang.builder.EqualsBuilder.ReflectionQuals)。但实际上比较JSON输出可能更有用。
Feature: Cucumber Feature 1

  Scenario: Test 1
    Given my micro-service is up and running
    When I submit something to my API
    Then I verify the response object looks like this:
      | property1 | value1 |
      | property3 | value3 |
      | property5 | value5 |
public class StepDefinitions {

    private ResponseObject storedResponseObject;

    @Given("^my micro-service is up and running$")
    public void given() throws Throwable {
        ...
    }

    @When("^I submit something to my API$")
    public void when() throws Throwable {
        storedResponseObject = postSomethingToAPI();
    }

    @Then("^I verify the response object looks like this:$")
    public void then(Map<String, String> gherkinMap) throws Throwable {
        ObjectMapper objectMapper = new ObjectMapper();
        ResponseObject expectedResponseObject = objectMapper.convertValue(gherkinMap, ResponseObject.class);
        ResponseObject actualResponseObject = storedResponseObject;
        Assert.assertEquals(expectedResponseObject, actualResponseObject);
    }
}
public class ResponseObject {

    private String property1;
    private String property2;
    private String property3;
    private String property4;
    private String property5;

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    public String getProperty2() {
        return property2;
    }

    public void setProperty2(String property2) {
        this.property2 = property2;
    }

    public String getProperty3() {
        return property3;
    }

    public void setProperty3(String property3) {
        this.property3 = property3;
    }

    public String getProperty4() {
        return property4;
    }

    public void setProperty4(String property4) {
        this.property4 = property4;
    }

    public String getProperty5() {
        return property5;
    }

    public void setProperty5(String property5) {
        this.property5 = property5;
    }
}