解析SOAP响应消息java

时间:2014-06-02 13:54:51

标签: java validation soap xpath rest-assured

我有以下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);

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);
相关问题