0
我有一个API POST端点返回结构类似于这样一个JSON对象:放置在具有嵌套值的JSON响应一个Java POJO
- 数据(I发送了原始数据)
- JsonAPI(该JSON的API版本端点符合)
- 状态(通话状态到端点)
- 消息(用于在架构验证错误的情况下)
的这些都是使用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"}
。
我的目标是将此值存储为子(?)对象。而且,而不是在SupplierResponseTest中的api值为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());
}
}