Monday, April 28, 2014

SOAP: Unit Testing with Spring Integration and Spring WS

Integration Testing Support in Spring WS 2.0:
Spring WS 2.0 provides an integration test framework, which will help you test the content of the message.
It provides support for testing both client-side (written using WebServiceTemplate) and server-side (having @Endpoint annotation) code. The best part of using this framework is that you do not have to deploy on the actual server to do the integration testing. It provides a mock server (MockWebServiceServer) for the client side and a mock client (MockWebServiceClient) for the server side. Let's write the client and server-side integration test for our service.

Add spring ws test dependency:

    <!-- For Testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-test</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

Create Client Test using MockWebServiceClient:
package com.fpt.webservices.endpoint;

import static org.springframework.ws.test.server.RequestCreators.withPayload;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/application-context-ws-TEST.xml" })
public class RoleEndPointTest {

    @Autowired
    private ApplicationContext applicationContext;

    private MockWebServiceClient mockClient;

    @Before
    public void createClient() {
        Assert.assertNotNull(applicationContext);
        mockClient = MockWebServiceClient.createClient(applicationContext);
    }

    @Test
    public void testGetRoleResponse() throws IOException {

        String request = "<ns2:RoleRequest xmlns:ns2='urn:springws:springwsservices'>"
                + "        <ns2:roleId>1</ns2:roleId>"
                + "        <ns2:roleName>roleName</ns2:roleName>"
                + "     </ns2:RoleRequest>";

        Source responsePayload = new StringSource(
                "<ns2:RoleResponse xmlns:ns2='urn:springws:springwsservices'>"
                        + "         <ns2:RoleDetails>"
                        + "            <ns2:roleId>1</ns2:roleId>"
                        + "            <ns2:roleName>roleName</ns2:roleName>"
                        + "         </ns2:RoleDetails>"
                        + "      </ns2:RoleResponse>");

        Resource schema = new FileSystemResource("src/main/resources/springws.xsd");

        mockClient.sendRequest(withPayload(new StringSource(request)))
                .andExpect(payload(responsePayload))
                .andExpect(ResponseMatchers.validPayload(schema));
    }
    
    /**
     * For validation
     * 
     * @throws IOException
     */
    @Test
    public void testGetRoleResponseInvalidPayLoad() {

        String request = "<ns2:RoleRequest xmlns:ns2='urn:springws:springwsservices'>"
                + "        <ns2:roleId>roleId</ns2:roleId>"
                + "        <ns2:roleName>roleName</ns2:roleName>"
                + "     </ns2:RoleRequest>";

        Source responsePayload = new StringSource(
                "<ns2:RoleResponse xmlns:ns2='urn:springws:springwsservices'>"
                        + "         <ns2:RoleDetails>"
                        + "            <ns2:roleName>roleName</ns2:roleName>"
                        + "         </ns2:RoleDetails>"
                        + "      </ns2:RoleResponse>");

        mockClient.sendRequest(withPayload(new StringSource(request)))
                .andExpect(payload(responsePayload));
    }
}

Create Client Test using MockWebServiceClient:

package com.fpt.webservices.common;

import java.math.BigInteger;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/application-context-ws-TEST.xml" })
public class RoleClientIntegrationTest {
    @Autowired
    private RoleClient roleClient;
    
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    private MockWebServiceServer mockWebServiceServer;

    @Before
    public void createServer(){
        mockWebServiceServer = MockWebServiceServer.createServer(webServiceTemplate);
    }
    
    @Test
    public void getRoleInfo(){
        String request = "<ns2:RoleRequest xmlns:ns2='urn:springws:springwsservices'>"
                + "        <ns2:roleId>1</ns2:roleId>"
                + "        <ns2:roleName>role name</ns2:roleName>"
                + "     </ns2:RoleRequest>";

        Source responsePayload = new StringSource(
                "<ns2:RoleResponse xmlns:ns2='urn:springws:springwsservices'>"
                        + "         <ns2:RoleDetails>"
                        + "            <ns2:roleId>1</ns2:roleId>"
                        + "            <ns2:roleName>role name</ns2:roleName>"
                        + "         </ns2:RoleDetails>"
                        + "      </ns2:RoleResponse>");
        
        mockWebServiceServer.expect(RequestMatchers.payload(new StringSource(request))).andRespond(ResponseCreators.withPayload(responsePayload));
        
        Role role = roleClient.getRoleInfo();
        Assert.assertEquals(BigInteger.ONE, role.getRoleId());
    }
}


Source Code

No comments:

Post a Comment