Is there a possibility of using REST Assured library for testing SOAP webservices? I have a bunch of Test Suites in SOAP UI and I need to check if there is possibility of using REST Assured. Can anyone suggest if this is possible? Many thanks for any comments.
-
1I found you've asked same question here: stackoverflow.com/questions/44968999/…. Please, do not cross-post a question on multiple Stack Exchange sites. More explanation and how to migrate questions you can find here: meta.stackexchange.com/questions/64068/…– dzieciouCommented Jul 9, 2017 at 6:43
-
1Sorry for that, I'm new at StackExchange and still learning rules here ;) Thanks for sharing link about how to cross-posting questions on Stack.– Monika ŁyżwaCommented Jul 10, 2017 at 9:18
3 Answers
Yes it is possible. I have written this code and it is working form me:
Created a file SoapRequestFile.xml with the content:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetConversionRate>
<!--Optional:-->
<tem:CurrencyFrom>USD</tem:CurrencyFrom>
<!--Optional:-->
<tem:CurrencyTo>INR</tem:CurrencyTo>
<tem:RateDate>2018-12-07</tem:RateDate>
</tem:GetConversionRate>
</soapenv:Body>
</soapenv:Envelope>
Then here is code I have written in eclipse:
@Test
public void postMethod() throws Exception {
FileInputStream fileInputStream = new FileInputStream(new File(".\\SOAPRequest\\SoapRequestFile.xml"));
RestAssured.baseURI="http://currencyconverter.kowabunga.net";
Response response=given()
.header("Content-Type", "text/xml")
.and()
.body(IOUtils.toString(fileInputStream,"UTF-8"))
.when()
.post("/converter.asmx")
.then()
.statusCode(200)
.and()
.log().all()
.extract().response();
XmlPath jsXpath= new XmlPath(response.asString());//Converting string into xml path to assert
String rate=jsXpath.getString("GetConversionRateResult");
System.out.println("rate returned is: " + rate);
}
Yes, it is possible, however not necessarily easy.
SOAP Web service is (usually) receiving HTTP requests with XML in the body and sending back HTTP responses with XML in the body.
REST-assured can send HTTP requests with any content, including XML. It can also verify XML in responses with GPath.
Here's an example from REST-assured site:
Imagine that a POST request to http://localhost:8080/greetXML returns:
<greeting> <firstName>{params("firstName")}</firstName> <lastName>{params("lastName")}</lastName> </greeting>
i.e. it sends back a greeting based on the
firstName
andlastName
parameter sent in the request. You can easily perform and verify e.g. thefirstName
with REST assured:given(). parameters("firstName", "John", "lastName", "Doe"). when(). post("/greetXML"). then(). body("greeting.firstName", equalTo("John")).
The problem is that SOAP Requests and SOAP Responses are usually far more complex and you will need to understand WSDL descriptors yourself.
REST-assured does not have direct support for testing SOAP services, but it is possible by manually setting the SOAPAction
and Content-Type
headers and doing an HTTP POST
etc. Then you can run XPath assertions on the response like you do for normal REST services in REST-assured.
I suggest you also evaluate Karate as it has built-in support for SOAP, and also makes XML manipulation a lot easier.