![](/img/trans.png)
[英]How to compare two Objects in Java that also have lists in its properties
[英]How do I compare two objects in Java where the expected objects properties are dynamic
我正在尋找一種基於Cucumber / Gherkin格式編寫的動態屬性來比較Java對象的方法。
有沒有人實現過這樣的事情或知道可以實現此目的的框架?
這是我要執行的操作的一個示例:
黃瓜功能
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;
}
}
注意-Assert.assertEquals()顯然不起作用。
有什么想法嗎?
謝謝,
本:)
ResponseObject
equals
方法。 在覆蓋方法中實現比較邏輯。
編輯
正如@Wietlol所注意到的,也要覆蓋hashcode
方法。 有關更多信息,請參見此處。 在Java中,為什么equals()和hashCode()必須一致?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.