简体   繁体   English

我如何在Java中比较两个对象,这些对象的预期对象属性是动态的

[英]How do I compare two objects in Java where the expected objects properties are dynamic

I'm looking for a way to compare objects in Java based on dynamic properties written in Cucumber/Gherkin format. 我正在寻找一种基于Cucumber / Gherkin格式编写的动态属性来比较Java对象的方法。

Has anyone implemented anything like this or knows of a framework which can achieve this? 有没有人实现过这样的事情或知道可以实现此目的的框架?

Here is an example of what I'm trying to do: 这是我要执行的操作的一个示例:

cucumber.feature 黄瓜功能

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 |

StepDefinitions.java StepDefinitions.java

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);
    }
}

ResponseObject.java ResponseObject.java

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;
    }
}

Note - The Assert.assertEquals() clearly isn't going to work. 注意-Assert.assertEquals()显然不起作用。

Any thoughts? 有什么想法吗?

Thanks, 谢谢,

Ben :) 本:)

Override equals method in ResponseObject . ResponseObject equals方法。 In overrided method realize comparing logic. 在覆盖方法中实现比较逻辑。

EDIT 编辑

As @Wietlol noticed, override hashcode method too. 正如@Wietlol所注意到的,也要覆盖hashcode方法。 More about this you can read here In Java, why must equals() and hashCode() be consistent? 有关更多信息,请参见此处。 在Java中,为什么equals()和hashCode()必须一致?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM