0

我有以下 SOAP 响应消息,如果项目导入成功,我需要验证响应代码是否为 1。如何使用 RestAssured 和 Java 做到这一点?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<env:Envelope 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Header/>

<env:Body>
    <n1:importProjectResponse 
        xmlns:n1=" some text here......" 
        xmlns:n2="..some text here...." 
        xsi:type="n2:ArrayOfProjectImportResultCode">
        <n2:ProjectImportResultCode>
            <n2:code>1</n2:code>
            <n2:message>Project 'test1' import was successful.</n2:message>
        </n2:ProjectImportResultCode>
    </n1:importProjectResponse>
</env:Body></env:Envelope>

我正在使用 RestAssured 进行测试,如果我可以通过利用 RestAssured 而不是编写更多类或方法来完成看似简单的任务来避免代码膨胀,那就太好了。有任何想法吗?

到目前为止我尝试过,这当然不起作用..

response  = given().
    auth().basic(USER, PASSWORD).
    body(request).
    headers("Content-type", "text/xml").
    expect().
        statusCode(200).
        body("//n2:code/text()", is("1")). // does not match .. hmmm :(
    when().post(URL);
4

2 回答 2

0

所以我从 RestAssured 支持论坛得到的答案是,我可以尝试 GPath,而不是 xpath。这对我有用:

response  = given().
auth().basic(USER, PASSWORD).
body(request).
headers("Content-type", "text/xml").
expect().
    statusCode(200).
    body("Envelope.Body.importProjectResponse.ProjectImportResultCode.code[0]", is("1")).
when().post(URL);
于 2014-06-03T15:18:28.313 回答
0

这个应该工作//*[name()='n2:code']/text()

于 2014-06-03T10:27:25.207 回答