Showing posts with label mockrunner. Show all posts
Showing posts with label mockrunner. Show all posts

Friday, February 8, 2013

SOAPUI MockServices and Groovy Response

Suppose you have a request like this:


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <IdentifyCustomer>
         <WebIdentification>
            <Email>vernetto@yahoo.com</Email>
            <Password>ciao</Password>
         </WebIdentification>
      </IdentifyCustomer>
   </soapenv:Body>
</soapenv:Envelope>



and you must return this response whenever the email contains "@yahoo.com":

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <IdentifyCustomerResponse>
         <!--You have a CHOICE of the next 2 items at this level-->
         <IdentificationSuccessful>
            <MarketID>2</MarketID>
            <CustomerID>123456</CustomerID>
         </IdentificationSuccessful>
      </IdentifyCustomerResponse>
   </soapenv:Body>
</soapenv:Envelope>   


and this response whenever the email contains "@gmail.com":

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <IdentifyCustomerResponse>
         <IdentificationFailed>
            <ErrorCode>CUSTOMER_NOT_FOUND</ErrorCode>
            <ErrorDescription>we cannot find this customer</ErrorDescription>
         </IdentificationFailed>
      </IdentifyCustomerResponse>
   </soapenv:Body>
</soapenv:Envelope>


You can create a MockService and customize the MockResponse http://www.soapui.org/Getting-Started/mock-services/4-Customizing-a-MockResponse.html

this is the script:


def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( mockRequest.requestContent ) 
def email = holder.getNodeValue("//Email")

if (email.contains("@yahoo.com")) {
 context.setProperty( "myresponse", "<IdentificationSuccessful><MarketID>2</MarketID><CustomerID>123456</CustomerID></IdentificationSuccessful>" )
}
else {
 context.setProperty( "myresponse", "<IdentificationFailed><ErrorCode>CUSTOMER_NOT_FOUND</ErrorCode><ErrorDescription>we cannot find this customer</ErrorDescription></IdentificationFailed>" )
}



and this the Groovy Response


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <IdentifyCustomerResponse>
  ${myresponse}
      </IdentifyCustomerResponse>
   </soapenv:Body>
</soapenv:Envelope>


Tuesday, March 29, 2011

Struts Royally Sucks

I have heard that 100s of times, and I have vague memories of myself struggling, some 7 years ago, against Struts quirks and all those HORRIBLE tag libraries (writing code in XML? Noooooo way.).

I have even hoped to be able to mock Struts, instead of having to deploy in container,
using this http://strutstestcase.sourceforge.net/ framework...

I use MockStrutsTestCase in a very basic setup and I get this:


java.lang.UnsupportedOperationException: getPathTranslated operation is not supported!
at servletunit.HttpServletRequestSimulator.getPathTranslated(HttpServletRequestSimulator.java:635)



no matter what I try...

So I am switching to MockRunner which so far seems to work.

There are also plugins for JUnit and Maven:
http://struts.apache.org/2.2.1/docs/struts-2-junit-plugin-tutorial.html
http://struts.apache.org/2.0.14/docs/testng-plugin.html


This framework is THE BLACK WIDOW MAKER of Front End Developers.


My advice: just throw Struts in the toilet and flush, you will immediately feel an intense relief. Then implement your Front End in Spring MVC or Vaadin.





Here more reasons why Struts sucks.