Saturday, 23 March 2013

Steps for Creating a Soap WebService mock project Using soapUI


Creating a soap web service mock project using soapUI

Steps for creating mock project

  1. Open Soapui and select New SoapUI project.
  2. A popup will appear like below:-





    3.  Fill the Required details as below after targeting the WSDL file path.






    StudService.wsdl File Content:-

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://localhost:7070/gaurav/springws/ContractFirstStudentService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:7070/gaurav/springws/ContractFirstStudentService" targetNamespace="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
      <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://localhost:7070/gaurav/springws/ContractFirstStudentService">

                <element name="studentRequest">
                            <complexType>
                                        <sequence>
                                                    <element name="id" type="string"/>
                                        </sequence>
                            </complexType>
                </element>
                <element name="studentResponse">
                            <complexType>
                                        <sequence>
                                        <element name="studentdetails" type="tns:studentdetails"/>
                                        </sequence>
                            </complexType>
                </element>
                <complexType name="studentdetails">
                            <sequence>
                                        <element name="name" type="string"/>
                                        <element name="total" type="string"/>
                                        <element name="address" type="tns:address"/>
                                       
                            </sequence>
                </complexType>
                <complexType name="address">
                            <sequence>
                                       
                                        <element name="flatNo" type="string"/>
                                        <element name="street" type="string"/>
                                        <element name="city" type="string"/>
                                        <element name="state" type="string"/>
                                        <element name="pin" type="string"/>
                            </sequence>
                </complexType>
    </schema>
      </wsdl:types>
      <wsdl:message name="studentRequest">
        <wsdl:part element="tns:studentRequest" name="studentRequest"/>
      </wsdl:message>
      <wsdl:message name="studentResponse">
        <wsdl:part element="tns:studentResponse" name="studentResponse"/>
      </wsdl:message>
      <wsdl:portType name="StudService">
        <wsdl:operation name="student">
          <wsdl:input message="tns:studentRequest" name="studentRequest"/>
          <wsdl:output message="tns:studentResponse" name="studentResponse"/>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="StudServiceSoap11" type="tns:StudService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="student">
          <soap:operation soapAction=""/>
          <wsdl:input name="studentRequest">
            <soap:body use="literal"/>
          </wsdl:input>
          <wsdl:output name="studentResponse">
            <soap:body use="literal"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="StudServiceService">
        <wsdl:port binding="tns:StudServiceSoap11" name="StudServiceSoap11">
          <soap:address location="http://localhost:7070/ContractFirstStudentService/services"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>


    4)      After browsing the wsdl file path in Initial WSDL/WADL option
                Check mark the Create sample Requests and Create TestSuite options.
    5)  After clicking on OK below popup will appear.


     

6) Click on OK, again a popup will appear, which is as follows:-

7)   Click on OK.

8)      Now Project will appear as below:-  



9)      Select the project StudentSoapUIWebServiceMocking and right click on it:-



10)  Select the option New MockService. After selection a popup will appear as below :-



11)   Fill this text box with any name like below and click on OK.  


12)   Right Click on StudentMockService and select the option New MockOperation


 

13)   A popup will appear like below, Click on OK.



14) Right Click on student operation and select New MockResponse option.




15)   Provide the Mock Response names as per the requirement, I kept the names like SuccessMockResponse and FailureMockResponse.

16)  Double click on the student operation, A window will be opened like below-
                               Select dispatch type as Script, as by default it is SEQUENCE



17) Write the groovy script as per the Response presentation like below:- 



import groovy.util.XmlSlurper

            def parsedContent = new XmlSlurper().parseText(mockRequest.requestContent)

            //dispatch based on request student id
           
            def studId = parsedContent.Body.studentRequest.id.toString()
           
            def responseToUse = "";

            log.info "studId is->"+studId

            if (studId.equals("0") || studId.equals("")) {
                        responseToUse = "FailureMockResponse"
            }

            else{
                         responseToUse = "SuccessMockResponse"
                        }
            return responseToUse
                       
  18) The Screen will appear as below:-



19) Fill the Required details in the Success and Failure mock responses as per the 

                   response representation. 


Success Mock Response is as below:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
   <soapenv:Header/>
   <soapenv:Body>
      <con:studentResponse>
         <con:studentdetails>
            <con:name>Gaurav</con:name>
            <con:total>1000</con:total>
            <con:address>
               <con:flatNo>FLAT No-XXX</con:flatNo>
               <con:street>DENIZEN COMPLEX</con:street>
               <con:city>HYDERABAD</con:city>
               <con:state>ANDHRA PRADESH</con:state>
               <con:pin>500034</con:pin>
            </con:address>
         </con:studentdetails>
      </con:studentResponse>
   </soapenv:Body>
</soapenv:Envelope>

            Failure Mock response is as below:-
           
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://localhost:7070/gaurav/springws/ContractFirstStudentService">
   <soapenv:Header/>
   <soapenv:Body>
      <con:studentResponse>
         <con:studentdetails>
            <con:name>INVALID NAME</con:name>
            <con:total>NOT FOUND</con:total>
            <con:address>
               <con:flatNo>N/A</con:flatNo>
               <con:street>N/A</con:street>
               <con:city>N/A</con:city>
               <con:state>N/A</con:state>
               <con:pin>N/A</con:pin>
            </con:address>
         </con:studentdetails>
      </con:studentResponse>
   </soapenv:Body>
</soapenv:Envelope>

20) Right Click on StudentMockService and select option Show MockService  Editor .                 A window will appear like below:-



21)  From the above window, Select the option Sets options for this MockService. In the appeared dialog box fill the reuired details like below:-


Note:- We can place any path and port-number(empty portnumber not using by any                                   services).
   
22)  Double click on the student request and select edit current endpoint in Address bar.   

            http://<hostname>:<MockService Running PortNumber>/path declared in mockservice     above.




23)  Send the reuired parameters in student request, here I need to send student id and I am sending student id as – 5 as per the groovy script if i will send student id as – 5 or any number then i should get success mock response or if i will send student id as – 0 or blank then i should get fail mock response.

24)   Start the StudentMockService on the specified port and endpoint by clicking on Green Button.


25) After Sending the student request proper success mock response will appear as  below:-

                          


26)  After Sending the student request for fail, mock response will appear as below:-   



27)  When student id is sent as blank in student request, we will get fail mock response:-



No comments:

Post a Comment