July 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Integration Testing Support in Spring WS 2.0, Page 2

  • March 15, 2011
  • By Shekhar Gulati
  • Send Email »
  • More Articles »

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.

Client-side Integration Testing

The main class for writing integration tests on the client side is MockWebServiceServer. This server accepts a request message, verifies it against the expected request messages, and then returns the response message.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("profile-service-client.xml")
public class ProfileServiceClientIntegrationTest {

private String request = "<ns2:UserProfileCreateRequest xmlns:ns2='http://shekhar.com/usermanagement/schemas'> Shekhar Gulati 27 ";
private String response = "<ns2:UserProfileCreateResponse xmlns:ns2='http://shekhar.com/usermanagement/schemas'> user created successfully ";

@Autowired
private WebServiceTemplate webServiceTemplate;
private MockWebServiceServer server;
@Autowired
private ProfileServiceClient client;

@Before
public void setup() {
server = MockWebServiceServer.createServer(webServiceTemplate);//1
}

@Test
public void testInvokeProfileServiceAndGetASuccessResponse() {
server.expect(RequestMatchers.payload(new StringSource(request))).andRespond(ResponseCreators.withPayload(new StringSource(response)));//2
Assert.assertEquals("user created successfully",client.invokeProfileServiceAndGetASuccessResponse());//3
server.verify();//4
}

}

The flow is :

  1. Create a MockWebServiceServer using a WebServiceTemplate. You can also create MockWebServiceServer using WebServiceGatewaySupport or using ApplicationContext.
  2. Set up request expectations using RequestMatcher and return response using ResponseCreator.
  3. Make a client call.
  4. Call the verify method to make sure all the expectations are met.

There are more tests that you can perform using MockWebServiceServer, such as testing for SoapFaultException, performing validation, etc. The mock server uses RequestMatcher to match the expected request and uses ResponseCreator to create the response to be sent back to client. You can also write your own matcher and creator by implementing the RequestMatcher and ResponseCreator interfaces. All the default implementations exist in the RequestMatchers and ResponseCreators utility classes.

@Test
public void shouldInvokeProfileServiceAndGetASuccessResponse()
throws Exception {
Resource schema = new FileSystemResource(
"src/main/webapp/WEB-INF/userManagement.xsd");
server.expect(RequestMatchers.payload(new StringSource(request)))
.andExpect(RequestMatchers.validPayload(schema))
.andRespond(
ResponseCreators
.withPayload(new StringSource(response)));
Assert.assertEquals("user created successfully",
client.invokeProfileServiceAndGetASuccessResponse());
server.verify();
}

@Test(expected = RuntimeException.class)
public void shouldThrowRuntimeException() {
server.expect(RequestMatchers.payload(new StringSource(request)))
.andRespond(
ResponseCreators.withException(new RuntimeException()));
client.invokeProfileServiceAndGetASuccessResponse();
server.verify();
}

@Test(expected = SoapFaultClientException.class)
public void shouldThrowSoapFault() {
server.expect(RequestMatchers.payload(new StringSource(request)))
.andRespond(
ResponseCreators.withServerOrReceiverFault(
"Soap Fault Exception", Locale.US));
client.invokeProfileServiceAndGetASuccessResponse();
server.verify();
}

Server-side Integration Testing

The main class for writing integration tests on the server side is MockWebServiceClient. The client creates a request message, sends the request to the service endpoint, which processes the request, and creates a response message. The client then receives the response message and verifies it against the expected response. It makes it very easy to test your SOAP service without deploying on the server and without the need for a client.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-ws-servlet-test.xml")
public class UserProfileEndpointIntegrationTest {

@Autowired
private ApplicationContext applicationContext;

private MockWebServiceClient mockClient;

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

@Test
public void testCreate() {
String request = "<ns2:UserProfileCreateRequest xmlns:ns2='http://shekhar.com/usermanagement/schemas'> Shekhar Gulati 27 ";
String response = "<ns2:UserProfileCreateResponse xmlns:ns2='http://shekhar.com/usermanagement/schemas'> user created successfully ";

Assert.assertNotNull(applicationContext);
mockClient
.sendRequest(
RequestCreators.withPayload(new StringSource(request)))
.andExpect(ResponseMatchers.payload(new StringSource(response)));
}

}

 

The mock client uses RequestCreator to create the request to be sent to the server and uses ResponseMatcher to match the response to be created by server. You can also write your own creator and matchers by implementing RequestCreator and ResponseMatcher interfaces. All the default implementations exist in RequestCreators and ResponseMatchers utility classes.

Conclusion

The introduction of an integration testing framework in Spring WS 2.0 is a very useful addition. It makes writing integration tests for both client- and server-side code very easy.

Download the Code

Download the profileService code from my github repository.

About the Author

Shekhar Gulati -- Contributing Editor, Java -- is a Java consultant with over 5 years experience. He currently works with Xebia India, an Agile Software Development company. The opinions in this article and on his blog are his own and do not necessarily represent the opinions of his employer. His own blog is at and you can follow him on twitter here.


Tags: testing, Spring

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel