2017-09-05 17 views
0

我有一個xml字符串,我想只比較選定的節點值,並在結果上聲明我的測試用例。 例如:我在XML字符串如何僅匹配xml的選定節點值?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <ns2:requestTransferResponse xmlns:ns2="http://external.interfaces.my.company.com/"> 
     <return> 
      <ersReference>2017051017472278401000061</ersReference> 
      <resultCode>0</resultCode> 
      <resultDescription>SUCCESS</resultDescription> 
      <receivedAmount> 
       <currency>BSD</currency> 
       <value>5500.00</value> 
      </receivedAmount> 
      <receiverPrincipal> 
       <principalId> 
        <id>SUBDIST1-1</id> 
        <type>RESELLERID</type> 
       </principalId> 
       <principalName>Sub Distributor 1</principalName> 
       <accounts> 
        <account> 
        <accountSpecifier> 
         <accountId>SUBDIST1-1</accountId> 
         <accountTypeId>RESELLER</accountTypeId> 
        </accountSpecifier> 
        <balance> 
         <currency>BSD</currency> 
         <value>985500.00</value> 
        </balance> 
        <creditLimit> 
         <currency>BSD</currency> 
         <value>0.00000</value> 
        </creditLimit> 
        </account> 
       </accounts> 
       <status>Active</status> 
       <msisdn>12420101000</msisdn> 
      </receiverPrincipal> 
      <requestedTransferAmount> 
       <currency>BSD</currency> 
       <value>5500</value> 
      </requestedTransferAmount> 
      <senderPrincipal> 
       <principalId> 
        <id>DIST1</id> 
        <type>RESELLERID</type> 
       </principalId> 
       <principalName>Distributor 1</principalName> 
       <accounts> 
        <account> 
        <accountSpecifier> 
         <accountId>DIST1</accountId> 
         <accountTypeId>RESELLER</accountTypeId> 
        </accountSpecifier> 
        <balance> 
         <currency>BSD</currency> 
         <value>1994429.24</value> 
        </balance> 
        <creditLimit> 
         <currency>BSD</currency> 
         <value>0.00000</value> 
        </creditLimit> 
        </account> 
       </accounts> 
       <status>Active</status> 
       <msisdn>12420100000</msisdn> 
      </senderPrincipal> 
     </return> 
     </ns2:requestTransferResponse> 
    </soap:Body> 
</soap:Envelope> 

下現在我想驗證其的XPath

1. //receiverPrincipal//balance//value i.e 985500.00 
2. //senderPrincipal//balance//value i.e. 1994429.24 

不需要其餘節點以下節點的值。所以我搜索了互聯網並找到了XmlUnit庫。任何人都可以提供一個關於如何使用它來驗證xpath節點值的提示嗎?

回答

0

如果您確實只需要一些可以通過XPath識別的選定文本,那麼XMLUnit的XPath支持應該可以工作。

漢譯XMLUnit測試的github上頁的例子(見https://github.com/xmlunit/xmlunit/#asserting-an-xpath-value)你的第一個要求

XPathEngine xpath = new JAXPXPathEngine(); 
String content = xpath.evaluate("//receiverPrincipal//balance/value/text()", source); 
assertEquals(985500.00, Double.valueOf(content), 1e-3); 

如果你真的肯定就會有一個只針對XPath的比賽。

使用Hamcrest匹配器它會是這樣的

assertThat(source, EvaluateXPathMatcher.hasXPath("//receiverPrincipal//balance/value/text(), 
    equalTo("985500.00"))