2017-07-10 39 views
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()); 
    } 


} 

回答

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


} 

宾果邦戈。固定。