This article extends the basic example described in "Web service testing with soapUI". It describes how to test and simulate exceptions.
Imagine we want to know the price of a trip from Berlin to Paris. But instead of typing "Berlin", we enter "Beriln". The FlightPriceService does not know any location named "Beriln" and throws an exception. In this case, we expect the TripPriceService to catch this exception and throw his own exception. That is what we are going to test.
1. Add a test case
- Right click on the test suite and select "New TestCase"
- Specify a name for the test case, for example "LocationNotFoundException TestCase"
- Click "OK"
- Set a timeout for the test case
2. Add a test request
- Right click in the test case editor and select "Append Step" -> "Test Request"
- Specify a name for the test request, for example "getTripPrice Test Request"
- Click "OK"
- Select the operation to invoke for the request "TripPriceServiceFacadeServiceSoapBinding -> getTripPrice"
- Click "OK"
- Select "Add SOAP Response Assertion", "Add Schema Assertion" and "Create optional elements"
- Make sure "Add not SOAP Fault Assertion" is not selected, because the exception we expect is a SOAP fault
- Click "OK"
- Edit the request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:trip="http://trip.price.service">
<soapenv:Header/>
<soapenv:Body>
<trip:getTripPrice>
<trip>
<adults>2</adults>
<duration>3</duration>
<from>Beriln</from>
<rooms>1</rooms>
<to>Paris</to>
</trip>
</trip:getTripPrice>
</soapenv:Body>
</soapenv:Envelope>
- Add a "SOAP Fault" assertion
- Add a "XPath Match" assertion, to check the faultstring
XPath Expression
declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/'; declare namespace ns1='http://trip.price.service'; //soap:Fault/faultstring
Expected Result
Location Beriln not found
3. Add a standard mock response
See the basic example for more details.
- Right click in the test case editor and select "Append Step" -> "Mock Response"
- Specify a name for the mock response, for example "getRoomPrice Mock Response"
- Click "OK"
- Select the "getRoomPrice" operation of the "HotelPriceServiceFacadeServiceSoapBinding" interface
- Set the port, in this example "8088"
- Set the path, in this example "/external-services-0.0.1-SNAPSHOT/webservices/HotelPriceService"
- Click "OK"
- Edit the response price
- Set "getTripPrice" as the start step
4. Add a mock response with an exception
- Right click in the test case editor and select "Append Step" -> "Mock Response"
- Specify a name for the mock response, for example "getFlightPrice Mock Response"
- Click "OK"
- Select the "getFlightPrice" operation of the "FlightPriceServiceFacadeServiceSoapBinding" interface
- Set the port, in this example "8088"
- Set the path, in this example "/external-services-0.0.1-SNAPSHOT/webservices/FlightPriceService"
- Click "OK"
- In the Mock Response editor click the "Creates a SOAP Fault Response" button (red question mark)
- Overwrite the current response with a fault
- Select "LocationNotFoundException" and click "OK"
- Edit the fault to set a faultcode and a faultstring
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:flig="http://external.services/flight">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soap:Server</faultcode>
<faultstring xml:lang="en">Location Beriln not found</faultstring>
<detail>
<flig:LocationNotFoundException/>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
- Set "getTripPrice" as the start step
Now you can run the test case, it should be successfull
Related topics:
"Web service testing with soapUI""Integration of soapUI tests in maven build cycle"
Technorati Tags: soapUI test web service exception
