簡體   English   中英

使Json模式與從API獲得的響應相匹配

[英]Matching the Json schema with the response getting from the API in rest-assured

我有一個發布API,我希望使用JsonSchemaValidator的方法,因為我希望整個響應都得到驗證,而不是通過執行斷言來選擇整個響應

我嘗試使用

  1. matchsJsonSchemaInClasspath(“我的文件名”)和
  2. matchsJsonSchema(我的文件對象)

我的回復即將成為現實,方法通過了,但是沒有對我的架構文件進行檢查或驗證

public void directLoginWihSchemaValiadtor(){
    File file = new File("C:/Users/abeey/git/SlingAppWebService/Configurations/JsonSchemaValidator_DirectLogin_AWS.json");
    jsonasmap.put("contactNo", "some number");
    jsonasmap.put("loginType","0");
    jsonasmap.put("appid","2");
    jsonasmap.put("co*****ode","IN");
    JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().
    setValidationConfiguration(ValidationConfiguration.newBuilder().freeze()).freeze();

    given().contentType(ContentType.JSON).body(jsonasmap).when().
    post("https://60i*****.execute-api.us-west-2.amazonaws.com/some-api").
    then().assertThat().
    body(JsonSchemaValidator.matchesJsonSchema(file))).


    log().all();
    jsonasmap.clear();
}

//body(JsonSchemaValidator.matchesJsonSchemaInClasspath("JsonSchemaValidator_DirectLogin_AWS.json").using(jsonSchemaFactory))

我試圖用jsonSchemaFactory來做到這一點,但是無論是設置為draftversion還是從哪里獲得它,我都沒有得到

我對此並不陌生,如果您發現此問題過於簡單以至於無法提出疑問,請接受我的意見

對於這種情況,通常我會執行以下操作:

  1. 使用模式生成器並為json主體創建模式(我使用此工具json-schema-generator

  2. 將生成的json模式放在類路徑中(例如test / resources)

  3. 將此代碼用作REST保證測試的一部分:

    。體(matchesJsonSchemaInClasspath( “your_schema_name.json”))

如果要確保模式驗證有效,則可以編輯模式文件並將任何必填字段的類型更改為其他內容,然后查看測試將失敗。

您可以參考我的這篇文章以查看一些代碼示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM