0

I'm looking for a way to compare objects in Java based on dynamic properties written in Cucumber/Gherkin format.

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

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

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.

Any thoughts?

Thanks,

Ben :)

2
  • Maybe using a dictionary instead of generic properties would be a good option. I leave you an example stackoverflow.com/questions/13543457/… Commented Jul 24, 2017 at 14:46
  • Some kind of Reflection comparer (e.g. org.apache.commons.lang.builder.EqualsBuilder.reflectionEquals). But it might actually be more useful to compare JSON output. Commented Jul 24, 2017 at 14:54

1 Answer 1

1

Override equals method in ResponseObject. In overrided method realize comparing logic.

EDIT

As @Wietlol noticed, override hashcode method too. More about this you can read here In Java, why must equals() and hashCode() be consistent?

Sign up to request clarification or add additional context in comments.

1 Comment

Also make sure to override the hashCode() method... and to make equals() take in an object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.