问题
I have an XML payload with the element ValidationFault
. Part of my validation is to confirm the ValidationFault
element occurs only once in the XML payload. Using the following Citrus Java DSL:
runner.receive(action -> action.endpoint(endpointName)
.validate("number://ValidationFault", 1));
I do not get back the expected value of 1, instead 0:
com.consol.citrus.exceptions.TestCaseFailedException: Validation failed: Values not equal for element '//ValidationFault', expected '1' but was '0'
I have manually confirmed that the response payload does contain the element in question. I also validated the XPath with an external tool and found that the XPath should be correct. I have tried with the namespaces as well, //soapenv:ValidationFault
and //:ValidationFault
, but I receive the same exception.
Edit:
This is the XML payload that is received (with some data removed):
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<SOAP-ENV:Body
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
<faultstring>ValidationFault</faultstring>
<faultactor>GetSalutation</faultactor>
<detail>
<ValidationFault
fault:retryable="false"
xmlns="http://domain/fault/2.0/"
xmlns:fault="domain/fault/2.0/">
<ErrorCode>flt-00001</ErrorCode>
<DescriptionText>A Schema Validation Failed</DescriptionText>
<Details>
<Text></Text>
</Details
<TransactionControlIdentificationID>
TBD
</TransactionControlIdentificationID>
<ZuluDateTime><ZuluDateTime>
</ValidationFault>
</detail>
</soapenv:Fault>
</SOAP-ENV:Body>
</soapenv:Envelope>
回答1:
You need to use a namespace context that declares the namespace prefixes for the Xpath expression evaluation:
receive(action -> action.endpoint(fooChannel)
.namespace("ns", "http://domain/fault/2.0/")
.validate("number:count(//ns:ValidationFault)", 1));
The Xpath expression evaluates to the node values by default. So please make sure to use count()
function in order to evaluate the number of elements.
As an alternative to that you could evaluate to the node-set and use the Hamcrest matcher hasSize()
:
receive(action -> action.endpoint(fooChannel)
.namespace("ns", "http://domain/fault/2.0/")
.validate("node-set://ns:ValidationFault", Matchers.hasSize(1)));
来源:https://stackoverflow.com/questions/50804863/citrus-xpath-validation-cannot-find-element