Monday 22 October 2012

Testing Web Services from JUnit using SoapUI

There is no doubt that SoapUI is a superb tool for testing Web Services. Yet, some people seem to think that SoapUI is just used for manual testing. In addition to manual tests, SoapUI supports automated tests, that is, test suites. Test suites are a step in the right direction towards automation but running them still requires human intervention. In other words, the user still has to launch SoapUI and click on the "Run..." button. This doesn't go down well in a CI environment.

Since SoapUI is a Java application, test suites can be invoked directly from JUnit removing the need for human intervention. On my GitHub page, I've uploaded a project that demonstrates how to do this. The project has a JUnit test case that launches a SoapUI mock Web Service and then invokes the test suite for it. Let me give a brief overview on how I went about developing the project.

The first step was to set up a SoapUI project and test suite based on a WSDL. There's plenty of documentation online on how to do this. In the test suite, I created a test case for each WSDL operation:


For every test case, I asserted that the SOAP response isn't a SOAP fault, complies with the schema, it's a valid SOAP response and the outcome of the operation is correct:


The next step was to create a mock Web Service in the same project so I have something to run the test suite against. Again, there is documentation out there on this.


Now it was time to start developing the JUnit test case. I could have imported into my Java project the SoapUI libraries from the SoapUI distribution. But being a sadist, I decided to use Maven : P. In the project's POM, I declared the repo where to find SoapUI and its dependencies:

I then declared the dependencies that I want to be retrieved and included in the classpath:

The last step was to write the test for my Web Service:

testCalculatorService is annotated with @Test which instructs the JUnit runner to execute the test. What the test does is simple:
  1. It loads the test suite and mock Web Service configurations from the SoapUI project calculator-soapui-project.xml
  2. Launch the mock Web Service
  3. Execute the test suite
You can run the test by typing in your console "mvn test" in the project root directory.

Friday 12 October 2012