Announcement Announcement Module
Collapse
No announcement yet.
Unit test using MockWebServiceClient Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 org.springframework.ws.test.server.MockWebServiceC lient;
    import org.springframework.ws.test.server.RequestCreators ;
    import org.springframework.ws.test.server.ResponseMatcher 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"
    (
    {
    "file:src/test/resources/applicationContext.xml"
    }
    )
    public class OcpEndpointTest extends Junit4MockTestCase {
    @Autowired
    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
    @Before
    public void setup() throws Exception {
    wsMockClient = MockWebServiceClient.createClient(applicationConte xt);
    }

    @Test
    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))
    .andExpect(ResponseMatchers.payload(response));
    }
    }


    applicationContext.xml

    Here is the all important applicationContext.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-3.0.xsd
    http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMe ssageFactory">
    <property name="messageFactory">
    <bean class="com.sun.xml.internal.messaging.saaj.soap.ve r1_1.SOAPMessageFactory1_1Impl"/>
    </property>
    </bean>

    <bean id="endPoint" class="full.package.name.Endpoint">
    <constructor-arg ref="add as many constructor arguments as needed" />
    </bean>

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

    <bean id="endpointMapping" class="org.springframework.ws.soap.addressing.serv er.SimpleActionEndpointMapping">
    <property name="mappings">
    <props>
    <prop key="http://.../ServicePing">endpoint</prop>
    </props>
    </property>
    </bean>

    <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>

    <bean class="org.springframework.ws.server.endpoint.adap ter.MarshallingMethodEndpointAdapter">
    <constructor-arg ref="jaxb2Marshaller"/>
    </bean>

    </beans>

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

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