2016-10-04 2 views
1

Ich habe einen Testfall, auf dem ich die Behauptung setzen möchte.Assert einen SoapUI Testfall mit Fehlergrund

Ich muss einen Grund für das Scheitern der Behauptung angeben.

Meine Ausgabe von XML-Format ist wie folgt:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
     <soapenv:Fault> 
     <faultcode>soapenv:Server</faultcode> 
     <faultstring>XYZ-001: input is wrong</faultstring> 
     <detail> 
      <con:fault xmlns:con="http://www.bea.com/wli/sb/context"> 
       <con:errorCode>XYZ-001</con:errorCode> 
       <con:reason>input is wrong</con:reason> 
       <con:location> 
        <con:node>PipelinePairNode1</con:node> 
        <con:pipeline>PipelinePairNode1_response</con:pipeline> 
        <con:stage>stage1</con:stage> 
        <con:path>response-pipeline</con:path> 
       </con:location> 
      </con:fault> 
     </detail> 
     </soapenv:Fault> 
    </soapenv:Body> 
</soapenv:Envelope> 

Mein gewünschtes Ergebnis der faultstring Knoten des xml sein sollte.

Dafür ich mit XPath Behauptung versucht haben, mit diesem Code:

declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/'; 
declare namespace con='http://www.bea.com/wli/sb/context'; 
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/') 

und ich habe wie erwartet Ausgang wahr. Nach dem Erzeugen JUnit Berichts wurde aus einem anderen Grunde geben:

Cancelling due to failed test step 

<h3><b>Failure Failed</b></h3><pre>[XPath Match] XPathContains comparison failed for path [declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/'; 
declare namespace con='http://www.bea.com/wli/sb/context'; 
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')], expecting [false], actual was [true] 
</pre><hr/> 

Ich gehe dann mit Groovy folgenden Skript:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def requsetHolder = groovyUtils.getXmlHolder(messageExchange.requestContent) 
def responseHolder = groovyUtils.getXmlHolder(messageExchange.responseContent) 
def refNum = responseHolder.getNodeValue("soapenv:Envelope/soapenv:Body/soapenv:Fault/") 
def testrunner = context.getTestRunner(); 
if (refNum != null){ 
    testrunner.fail("soapenv:Envelope/soapenv:Body/soapenv:Fault/faultstring") 
} 

aber kein Glück auch diesmal Zeit. junit Störungsursache war:

Cancelling due to failed test step 

<h3><b>Failure Failed</b></h3><pre>[Script Assertion] net.sf.saxon.trans.XPathException: XPath syntax error at char 46 on line 2 in {...pe/soapenv:Body/soapenv:Fau...}: 
Unexpected token "<eof>" in path expression 
</pre><hr/> 

so gibt es eine Möglichkeit, durch die ich meine kundenspezifischen Grund in junit Ausgangs Behauptung entweder stark oder XPath erzeugen kann.

+0

sieht aus, dass Sie 'expecting [false]' statt 'true' in der' Xpath Assertion' von Ihrer Beschreibung hinzugefügt haben? – Rao

+0

Nein, der Grund für das Fehlschlagen des Testfalls sollte der Grund sein, der im Fehler-Tag von xml erwähnt wird. Aber hier ist der Grund etwas anderes. Gibt es eine Möglichkeit, die benutzerdefinierte Fehlermeldung in Assert zu erwähnen, indem Sie entweder xpath oder groovy verwenden? – Sarvesh

+0

Hmm., Nicht klar. Möchten Sie weitere Details mit Beispiel hinzufügen? – Rao

Antwort

1

Basierend auf Ihrer Frage & Kommentare, hier ist die Script Assertion.

  • Das Skript enthält verschiedene Methoden zum Anzeigen der angepassten Nachricht im Bericht.
  • Bitte folgen Sie den Inline-Kommentaren für Details.
  • Beispielcode-Snippet hinzugefügt für zusätzliche Überprüfung auf spezifischen errorCode Element Wert, wenn die Antwort ein Fault ist. Sie können es auch für andere Elemente anwenden.

Script Assertion:

/** 
* The below script should be used as Script Assertion 
* which checks if the response contains Fault, raise error otherwise 
* Once it has fault in it, then it checks for the specific "errorCode", raise error with 
* customized message 
*/ 

//Get the response parsed 
def envelope = new XmlSlurper().parseText(context.response) 

//There are three approaches to check & and throw customized error message 
// if the response does not have Fault. Use one of them 
assert envelope.Body.Fault, "Response does not have soap fault" 
assert !envelope.Body.Fault.isEmpty(), "Response does not have soap fault" 
if (!envelope.Body.Fault) { throw new Error ("Response does not have soap fault") } 

//Further check for specific errorCode in the soap fault 
def expectedErrorCode = 'XYZ-001' 
def actualErrorCode = envelope.'**'.find {it.name() == 'errorCode' } as String 

log.info "Actual code is : $actualErrorCode" 
assert expectedErrorCode == actualErrorCode, "Soap fault should not have \"${expectedErrorCode}\"" 

Sie können es schnell von here testen direkt zu sehen, wie es sich verhält, wenn errorCode nicht übereinstimmt.

Verwandte Themen