September 12, 2008

SOAP Service Testing

« Zum Spring-CXF-Service Routing mit Apache Camel hinzufügen | Main | Camel-Routen visualisieren »

Recently I looked for an easy and trivial way for testing webservices at junit tests. One way is the jetty servlet container which is runnable as ambedded server at java source code.

(1) First you have to define the content of your soap request. An example could be:

   1:<soapenv:Envelope 
   2:  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   3:  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   4:  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   5:  <soapenv:Body>
   6:    <ns1:sayHello soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://127.0.0.1:40406/hello">
   7:      <integer href="#id0"/>
   8:    </ns1:sayHello>
   9:    <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
  10:      xsi:type="xsd:long" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Rafael</multiRef>
  11:  </soapenv:Body>
  12:</soapenv:Envelope>

(2) Equivalent Axis Service Call mechanism, that generates the soap content above:

JavaCall.java
   1:Call c = new Call(serviceURL + "/hello");
   2:c.addParameter("string", new QName("http://www.w3.org/2001/XMLSchema", "string"), ParameterMode.IN);
   3:c.setReturnType(new QName(serviceURL + "/Server", "string"));
   4:Object o = c.invoke(serviceURL + "/hello", "sayHello", new String[]{"Rafael"});
   5:String returnString = o.toString();

(3) Second you define the response soap content. An example could be:

soap-response.xml
   1:<?xml version="1.0" encoding="UTF-8"?>
   2:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   3:        <soapenv:Body>
   4:                <ns1:sayHello soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://127.0.0.1:40406/hello">
   5:                        <hello href="#id0"/>
   6:                </ns1:sayHello>
   7:                <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Server: Hello Rafael</multiRef>
   8:        </soapenv:Body>
   9:</soapenv:Envelope>

(4) Then you implement a simple servlet, which generated the expected response which generic content:

ServletTest.java
   1:public class SimpleServlet extends HttpServlet { 
   2:
   3:    public void doProcess (HttpServletRequest req, HttpServletResponse res)
   4:        throws ServletException, IOException {
   5:        ServletOutputStream out = res.getOutputStream();
   6:        res.setContentType("text/xml");
   7:        out.print("... response soap code snippet + generic code ...");
   8:        out.close();
   9:    }
  10:    
  11:    public void doGet (HttpServletRequest req, HttpServletResponse res)
  12:        throws ServletException, IOException {
  13:            doProcess(req, res);
  14:    }
  15:    
  16:    public void doPost (HttpServletRequest req, HttpServletResponse res)
  17:        throws ServletException, IOException {
  18:            doProcess(req, res);
  19:    }
  20:
  21:    public String getServletInfo() {
  22:        return "A simple servlet";
  23:    }
  24:}

(5) Last you have to implement the junit test:

UnitTest.java
   1:public class TestSoap {
   2:
   3:    @Test
   4:    public testSoap() throws Exception {
   5:        ServletTester tester = new ServletTester();
   6:        tester.setContextPath("/");
   7:        tester.addServlet(ServletTest.class, "/*");
   8:        String serviceURL = 
   9:            tester.createSocketConnector(true);
  10:        tester.start();
  11:        
  12:        String soapRequest = "... first snippet, which will generated from second java snippet ...";
  13:        
  14:        //second axis call snippet with the service url
  15:        
  16:        assertEquals("Server: Hello Rafael",response.getContent());
  17:    }
  18:} 

Regards Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:36 PM in SOA