简体   繁体   English

属性的RestAssured和GPath表达式不一致

[英]RestAssured and GPath expression for attribute not consistent

We use the REST-assured framework for doing some unit/integration testing in Java. 我们使用REST保证的框架在Java中进行一些单元/集成测试。

The XML answer from a REST service is similar to this: REST服务的XML答案与此类似:

<?xml version="1.0" encoding="UTF-8"?>
<Items xmlns="urn:service:com:namespace:item/1"
    returned="3" found="3">

    <ItemRef object="urn:svc:com:car:item:123456" type="door">door-123456.pdf</ItemRef>
    <ItemRef object="urn:svc:com:car:item:983425" type="mirror">mirror-43562577.pdf</ItemRef>
    <ItemRef object="urn:svc:com:car:item:983425" type="wheel" >door-94584854.pdf</ItemRef>    
</Items>

In my test I am interested to check the number of items returned by reading the attribute returned like this 在我的测试中,我有兴趣通过读取像这样returned的属性来检查返回的项数

givenOK()
    .expect()
        .body("Items.@returned", equalTo("3")) // this is a string
    .when()
    .get(myurl)

And it works well 而且效果很好

Now I want as well to control if the URN in the xmlns is correct with the same logic: 现在,我也想用相同的逻辑来控制xmlns中的URN是否正确:

givenOK()
    .expect()
        .body("Items.@returned", equalTo("3")) // this is a string
        .body("Items.@xmlns", equalTo("urn:service:com:namespace:item/1"))
    .when()
    .get(myurl)

But when my test run, the expression Items.@xmlns seems not returning the value of the attribute but empty: [] 但是当我运行测试时,表达式Items.@xmlns似乎没有返回该属性的值,但为空:[]

Any idea why this is not working? 知道为什么这行不通吗?

Could it be that the "xmlns" attribute is treated specially because it indicates a namespace? 难道是因为“ xmlns”属性表示名称空间而被特别对待?

A possible work-around would be to declare the namespace in the XmlConfig and verify something in the body. 可能的解决方法是在XmlConfig中声明名称空间并验证主体中的内容。

given().
        config(RestAssured.config().xmlConfig(XMLConfig.xmlConfig().declareNamespace("ns", "urn:service:com:namespace:item/1"))).
when().
        get(myUrl).
then().
        body("'ns:ItemRef'[0]", equalTo("door-123456.pdf"));

And another example with multiple nodes and attributes (explicit path): 另一个具有多个节点和属性(显式路径)的示例:

given()
    .config(
        RestAssured.config()
            .xmlConfig(XmlConfig.xmlConfig()
                .declareNamespace("ns", "urn:service:com:namespace:item/1"))).
    when()
        .get(myUrl)
    .then()
        .body("'ns:RootNode'.'ns:Level1'.'ns:Level2'[0].'@ns:id'", equalTo("AN-ID-123"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM