我有一个API POST端点,它返回一个结构如下的json对象:
所有这些都使用GSON fromJson():
存储在以下POJO中package json.responses;
import com.google.gson.JsonElement;
public class SupplierResponseTest {
private StatusResponse status;
private JsonElement jsonapi;
private String message;
private JsonElement data;
public SupplierResponseTest(StatusResponse status, JsonElement jsonapi) {
this.status = status;
this.jsonapi = jsonapi;
}
public SupplierResponseTest(StatusResponse status, JsonElement jsonapi, String message) {
this.status = status;
this.jsonapi = jsonapi;
this.message = message;
}
public SupplierResponseTest(StatusResponse status, JsonElement jsonapi, JsonElement data) {
this.status = status;
this.jsonapi = jsonapi;
this.data = data;
}
public StatusResponse getStatus() {
return status;
}
public JsonElement getJsonapi() {
return jsonapi;
}
public String getMessage() {
return message;
}
public JsonElement getData() {
return data;
}
}
如您所见,我目前将jsonapi值存储为JsonElement
。这意味着当我从json解析值时,我最终得到的字符串值为{"version":"1.0"}
。
我的目标是将此值存储为子(?)对象。并且,不是api值是SupplierResponseTest中的JsonElement
,而是将其作为对象或枚举。但是,我对如何做到这一点感到困惑。
以这种方式存储值的目的是能够对新对象的值1.0
执行黄瓜验证,而不是解析一堆json {"version":"1.0"}
。
我正在使用的黄瓜是:
Scenario Outline: Add Supplier Details
Given the system does not know about any Suppliers
When the client adds the following <supplier>
Then the response status is <status>
And the response has the <jsonapi> version
And the response has the request <supplier>
Examples:
| supplier | status | jsonapi |
| "Blue Network Energy LTD" | "SUCCESS" | "1.0" |
我遇到的具体步骤定义是:
@Then("^the response has the \"([^\"]*)\" version$")
public void the_response_has_the_version(String arg1) throws Throwable {
try{
//This returns a string of json...
JsonElement apiVersion = supplierResponse.getJsonapi();
//The below attempts to assert the two values "1.0" and {"version":"1.0"}
Assert.assertEquals(arg1, apiVersion.toString());
} catch (Exception e){
System.out.println(e.getMessage());
}
}
答案 0 :(得分:0)
好的,我从this question起了带头。
我创建了一个新类,它涵盖了嵌套的json元素:
package json.responses;
public class ApiVersionResponseTest {
private String version;
private ApiVersionResponseTest(String version) {
this.version = version;
}
public String getApiVersion() {
return version;
}
}
更改了父类构造函数以允许将apiversion传递并创建为新对象:
package json.responses;
import com.google.gson.JsonElement;
public class SupplierResponseTest {
private StatusResponseTest status;
private ApiVersionResponseTest jsonapi; //Relevant change
private String message;
private JsonElement data;
public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi) {
this.status = status;
this.jsonapi = jsonapi;
}
public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi, String message) {
this.status = status;
this.jsonapi = jsonapi;
this.message = message;
}
public SupplierResponseTest(StatusResponseTest status, ApiVersionResponseTest jsonapi, JsonElement data) {
this.status = status;
this.jsonapi = jsonapi;
this.data = data;
}
public StatusResponseTest getStatus() {
return status;
}
public ApiVersionResponseTest getJsonapi() {
return jsonapi;
}
public String getMessage() {
return message;
}
public JsonElement getData() {
return data;
}
}
步骤定义(现在正在工作)调用嵌套对象,如下所示:
@Then("^the response has the \"([^\"]*)\" version$")
public void the_response_has_the_version(String arg1) throws Throwable {
try{
Assert.assertEquals(arg1, supplierResponse.getJsonapi().getApiVersion());
} catch (Exception e){
System.out.println(e.getMessage());
}
}
Bingo bango。固定。