June 29, 2015

957 Views

Mock Testing using JUnit and SoapUI

An Overview of Mock Testing

Mock Testing is a unit testing technique, in which the testing of complex code /complex real objects with similar behavior of the expected application is needed. In mocking, real complex objects can be simulated using this method. This way, the desired functionality of the application can be tested without the actual running of real application objects.

In mock testing, real application objects used in the application under development can be simulated by mocking the objects, so that the application can be tested with respect to the used complex real application objects.

An Overview of SoapUI and JUnit

SoapUI is a tool which is used to test web service(s) and helps in mocking of web service(s) object(s). In SoapUI, a well implemented user interface has been provided, which helps in simulating the complex objects using mocking.    

JUnit is the unit testing tool available for testing Java based application(s). In JUnit, unit test cases of the application under development has to be made, and those test cases would execute automatically (i.e. automation testing).

How to achieve Mock Testing

To achieve mock testing, let us consider a scenario in which a Java based application uses an SDK (i.e. jar file) and requires the actual environment in which the SDK would execute. Due to the huge cost of an actual environment, the SDK won’t be possible to test. For a similar SDK, the “.wsdl” file is also available (i.e. a source of web service), without having the actual environment the “.wsdl” should be hosted over a dynamic port in SoapUI. Then the known method which has been implemented using .jar file has to be mocked in response of the SoapUI request.

  1. Example of the application for mock testing

    The VMWare vShpere SDK is the most suitable application in which mock testing could be used, because of the high cost associated with the VMWare environment.

  2. High level steps to achieve mock testing

    The following steps must be followed while doing mock testing:

    1. Create java source code using “vim25.jar” (i.e. SDK of VMWare vSphere for Java).
    2. Create mock service in SoapUI for “vim.wsdl” (i.e. Web service version of vim25.jar).
    3. Create response in SoapUI for the respective method.
    4. Start the mock service on dynamic port in SoapUI.
    5. Consume the mock service in JUnit.
    6. Call the similar “.wsdl” method instead of calling the “.jar” method in java source code.
    7. Execute the JUnit test case.

    The expected result, which we specified in response from the SoapUI mock service, would be returned as a result to the JUnit, which ensures that the code has been executed with respect to the calling method.

  3. Sample of SoapUI mocking

    The following XML snippet is the response of SoapUI mock service for vim.wsdl file:

The Benefits of Mock Testing

In terms of the benefits, mock testing serves the purpose of unit testing in the absence of an actual real environment. Using mock, the unit test serves the purpose of module testing and not for component testing.

References

  1. http://www.soapui.org/soap-mocking/working-with-mockservices.html
  2. http://www.soapui.org/getting-started/mock-services.html