RestAssured无法使containsString工作

时间:2017-10-16 20:57:01

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

这是回应(有点)

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

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

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

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

}

我需要验证响应是否包含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);

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

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

我是新人,放心,我错过了什么。 谢谢!

1 个答案:

答案 0 :(得分:0)

我认为其中一个问题是你有一个JSON响应,想要检查xml内容,就像你在"接受"中设置的标题一样。标题为" application / xml"。试试" application / json"并检查你的jsonpath,因为它也很臭。

如果你不能分享你的json,你试着通过http://jsonpath.com/评估并找到纠正jsonpath的方式,好像你可以在Cats数组下有多个记录,它应该看起来像第一个elem:&# 34;猫[0] ..令牌"