Documentbuilderfactory non validating

07-Aug-2015 02:36

For those of us involved in testing web services and SOA-based applications, there are lots of different test tools on the market that can help speed up the testing and make them repeatable and easy to maintain.Some examples of these tools are Soap UI from Smart Bear and SOAtest from Parasoft.Soap UI is available both as a freeware product and as an enterprise edition offering additional functionality. One of the biggest downsides of the freeware version of Soap UI is the lack of data driven test support.This feature is only available in the paid version.

documentbuilderfactory non validating-21

dating exposed surface contexts

As I am very regularly involved in web service testing myself, and often want to use data driven testing to create maintainable and flexible test suites, I thought it would be a good idea to see whether it’s possible to quickly create a very basic web service testing tool that supports data driven testing.For every test data row, we will then execute the following steps: private static SOAPMessage create SOAPRequest(String str Path) throws Exception { // Create a SOAP message from the XML file located at the given path File Input Stream fis = new File Input Stream(new File(str Path)); Message Factory factory = Message Instance(); SOAPMessage message = factory.create Message(new Mime Headers(), fis); return message; }private static SOAPMessage get SOAPResponse(SOAPMessage soap Request, String str Endpoint) throws Exception, SOAPException { // Send the SOAP request to the given endpoint and return the corresponding response SOAPConnection Factory soap Connection Factory = SOAPConnection Instance(); SOAPConnection soap Connection = soap Connection Factory.create Connection(); SOAPMessage soap Response = soap Request, str Endpoint); return soap Response; }private static void validate Value(SOAPMessage soap Msg, String str El, String str Expected) throws Exception { // Get all elements with the requested element tag from the SOAP message SOAPBody soap Body = soap SOAPBody(); Node List elements = soap Elements By Tag Name(str El); // Check whether there is exactly one element with the given tag if (Length() != 1){ println("Expected exactly one element " + str El + "in message, but found " + String(Length())); } else { // Validate the element value against the expected value String str Actual = elements.item(0)Text Content(); if (str Actual.equals(str Expected)) { println("Actual value " + str Actual + " for element " + str El + " matches expected value"); } else { println("Expected value " + str Expected + " for element " + str El + ", but found " + str Actual + " instead"); } } } Running the test When we run the test, we see that the web service to be tested is called three times.Two tests succeed, the last test case fails (on purpose, to show that the validation is executed properly): Extensions to our tool As stated earlier in this post, there are a lot of improvements to be made to this very basic web service test tool.

The test data source As in the previous example, let’s create a test data source first.

I chose to base the solution presented here on predefined XML request message files as this saves us the trouble of creating XML objects in our code.