-
Notifications
You must be signed in to change notification settings - Fork 3
Common Features
<wiki:toc max_depth="2" />
This page describes client test features common for all SOAP stacks. First of all, you should read Spring WS documentation on client side and server side testing.
Smock adds several capabilities and generalize the usage on more SOAP stacks but the principle is still the same.
First of all, you have to add Maven dependency to your project. If you use Spring WS add this net.javacrumbs smock-springws 0.7 test If you use another library, just change the artifactId accordingly.
Smock extends the original Spring WS testing framework in several ways. To be able to the extensions, you have to add the following static import to your test class
Client side import: net.javacrumbs.smock.springws.client.SmockClient.*;
Server side import: net.javacrumbs.smock.springws.server.SmockServer.*;
Again, if you use Axis2 just replace springws
by axis2
.
Smock adds support for whole SOAP messages while vanilla Spring WS supports only payloads. You can compare requests and generate responses based on messages containing SOAP envelope thus enabling you better control over your test.
Client side message support: mockServer.expect(anything()).andRespond(withMessage("response1.xml"));
Server side message support: client.sendRequest(withMessage("request1.xml")).andExpect(message("response1.xml"));
Please note, that if given message does not contain SOAP envelope it is treated as payload.
If the message()
method is used, ${IGNORE}
values are ignored. For example in request
<plusRequest xmlns="http://javacrumbs.net/calc">
<a>${IGNORE}</a>
<b>${IGNORE}</b>
</plusRequest>
values of elements a
and b
are ignored.
Usually it's convenient to store request and response XML in files. That's why Smock provides simplified resource access.
First of all, message
and withMessage
methods accept String attribute that represents a resource to load the message from.
Moreover, there are resource
and fromResource
methods that simplify resource loading.
client.expect(validPayload(resource("xsd/calc.xsd"))).andRespond(withMessage("response2.xml"));
Parametrization of request and response messages is often needed. Smock supports template mechanism. You can specify XSLT template as your message and Smock will take care of processing. Templates used by message
method have access to provided parameters, templates used by withMessage
can access parameters and the request.
Example: client.expect(validPayload(resource("xsd/calc.xsd"))) .andExpect(message("request-context-xslt.xml").withParameter("a",1).withParameter("b", 4)) .andRespond(withMessage("response-template.xml"));
request-context-xslt.xml: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="a"/> <xsl:param name="b"/> <xsl:template match="/"> <xsl:value-of select="$a"/> <xsl:value-of select="$b"/> </xsl:template> </xsl:stylesheet>
response-template.xml: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:value-of select="//c:a + //c:b"/> </xsl:template> </xsl:stylesheet>
Smock also supports templates written in Groovy. Te be able to use them, add following dependency to your project
<dependency>
<groupId>net.javacrumbs</groupId>
<artifactId>smock-groovy-template</artifactId>
<version>2.0</version>
</dependency>
Then you have to set up the template processor. It can be done either in the test constructor or in a static block.
static
{
setTemplateProcessor(new GroovyTemplateProcessor());
}
Now you can use templates written in Groovy
<plusResponse xmlns="http://javacrumbs.net/calc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result>${(plusRequest.a.text() as int) + (plusRequest.b.text() as int)}</result>
</plusResponse>
Sometimes it's needed to apply interceptors on the test side. For example if you want to test WS-Security. Since the interceptor setup is SOAP stack specific, please consult page dedicated to your stack.
If you want to simplify your tests even further, you can extend Smock provided base class. It takes care of bootstrapping and provides all necessary methods.
Simple client test public class CalcSimpleTest extends AbstractSmockClientTest {
private CalcClient calc = ...;//SOAP stack dependent code
@Test
public void testSimple() throws RemoteException
{
expect(anything()).andRespond(withMessage("response1.xml"));
long result = calc.plus(1, 2);
assertEquals(3, result);
}
@After
public void verify()
{
super.verify();
}
}
Simple server test public class SimpleEndpointTest extends AbstractSmockServerTest{
public SimpleEndpointTest() {
//SOAP stack specific code
}
@Test
public void testSimple() throws Exception {
sendRequest(withMessage("request1.xml")).andExpect(noFault());
}
}