Announcement Announcement Module
No announcement yet.
Unit test using MockWebServiceClient Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit test using MockWebServiceClient

    I just got my unit test running!

    And as I have found very examples of unit tests using MockWebServiceClient, I'm posting a skeleton framework of what I did to help promote the use of mock tests in Spring Web Services.

    The test runs under Maven 3.0.4

    Junit test:

    package <where ever you want to put it>

    import javax.xml.transform.Source;

    import junit.framework.Assert;
    import mil.disa.necc.ap.jcrm.mock.Junit4MockTestCase;

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.ContextConfigurat ion;
    import org.springframework.test.context.junit4.SpringJUni t4ClassRunner;
    import lient;
    import ;
    import s;
    import org.springframework.xml.transform.StringSource;

    * The first important thing to remember about mock tests is that they do not
    * run with the Spring Framework in the background like a regular SpringFramework
    * service does. So, all the neat "Spring stuff" needs to be invoked manually.
    * Put all the Spring Framework configuration "stuff" you need in a file called
    * applicationContext.xml.
    * Just make sure that the test can find the file when it runs.
    * You won't get very far if the test cannot load the file.

    @RunWith(SpringJUnit4ClassRunner.class) // Tie Spring into JUnit
    @ContextConfiguration // Identify the file, with all the important "Spring stuff"
    public class OcpEndpointTest extends Junit4MockTestCase {
    private ApplicationContext applicationContext;
    private MockWebServiceClient wsMockClient;

    // Before the test method runs, feed the file with the important "Spring stuff"
    // into the method to create the client
    public void setup() throws Exception {
    wsMockClient = MockWebServiceClient.createClient(applicationConte xt);

    public void testOcpSecurePing() {
    Assert.assertNotNull(applicationContext); // No point in running if you can't find the file

    Source request = new StringSource("<Put your expected xml response here. I got mine from a soapUI call.>");

    Source response = new StringSource("<Put your expected xml response here. Again, I got mine from a soapUI call.>");

    // Invoke the web service call
    wsMockClient.sendRequest(RequestCreators.withPaylo ad(request))


    Here is the all important applicationContext.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

    <bean id="messageFactory" class=" ssageFactory">
    <property name="messageFactory">
    <bean class=" r1_1.SOAPMessageFactory1_1Impl"/>

    <bean id="endPoint" class="">
    <constructor-arg ref="add as many constructor arguments as needed" />

    <!-- Map the call to an end point -->
    <bean class=" ing.PayloadRootAnnotationMethodEndpointMapping">
    <property name="defaultEndpoint" ref="endPoint" />

    <bean id="endpointMapping" class=" er.SimpleActionEndpointMapping">
    <property name="mappings">
    <prop key="http://.../ServicePing">endpoint</prop>

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
    <property name="contextPath" value="list of schema files comma separated" />
    <property name="supportJaxbElementClass" value="true" />

    <bean class=" ter.MarshallingMethodEndpointAdapter">
    <constructor-arg ref="jaxb2Marshaller"/>


    Actual web service:
    public class Endpoint extends BaseEndpoint {
    public Endpoint() {

    @PayloadRoot(localPart = "ServicePing", namespace = "http://...")
    public JAXBElement<ServicePingResponse> ping(final JAXBElement<ServicePing> pingDoc) {
    ServicePingResponse responseDoc = new ServicePingResponse();
    return OCP_OBJECT_FACTORY.createServicePingResponse(respo nseDoc);