【问题标题】:RestAssured Cannot make containsString workRestAssured 无法使 containsString 工作
【发布时间】:2017-10-16 20:57:01
【问题描述】:

这是响应(有点)

{
  "Cats": [
        {
        "Code": "XXXX",
        "Ven": {          
            "Description": "Any"
        },

        },
        "Rate": {
            "Actual": {
                "Amount": "XXX",
                "Currency": "USD"
            },
            "Token": "Cg..........................................",

       {
        "Code2": "2XXXX",
        "Vend": {
            "Code": "2X",
            "Description": "aNY2"
        },

            "Token": "Cg...................................",

}

我需要验证响应是否包含 Token 值,如果这个 token 值包含某个字符串,这就是代码

    Response responseshop = given().header("Authorization", "Basic 
    XXXXXXXXXXX=").header("accept-version","2.0.0").header("Accept", 
    "application/xml").
            param("Location", "Geocode,XX.XXXXXX,-XX.XXXXXX").
            param("Pick", "2017-12-12"). 
            param("Drop", "2017-12-15").            
            get("/cats/shop?AppKey=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); 
        responseshop.then().log().all().
            statusCode(200).            
                     body("root.Cats.Rate.Token", containsString("Cg")); 
            System.out.println("This is the Response");
            System.out.println(responseshop);             
            String Token =responseshop.path("root.Cats.Rate.Token");
           System.out.println("This is the Token");
           System.out.println(Token);

顺便说一句,响应被打印出来,它也通过了 200 状态验证, 但是我一遍又一遍地得到这个断言错误:

java.lang.AssertionError: 1 expectation failed.
XML path root.Cats.Rate.Token doesn't match.
Expected: a string containing "Cg"
Actual: 
Cg........................

我很放心,我是不是错过了什么。 谢谢!

【问题讨论】:

    标签: automation rest-assured web-api-testing


    【解决方案1】:

    我认为问题之一是您有一个 JSON 响应并想要检查 xml 内容,因为您在“Accept”标头中将其设置为“application/xml”。尝试使用“application/json”并检查您的 jsonpath,因为它也很臭。

    如果您无法共享您的 json,您尝试通过 http://jsonpath.com/ 评估并找到更正 jsonpath,就好像您可以在 Cats 数组下拥有多条记录一样,第一个 elem 应该是这样的:“Cats[0 ]..令牌”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2014-12-12
      • 2018-11-02
      • 2014-07-21
      • 2014-04-10
      相关资源
      最近更新 更多