November 22, 2014
Hot Topics:

Spring WS 2.0: New Integration Test Support

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

Spring WS 2.0 introduced a new integration testing module, spring-ws-test, which provides a fluent API for testing both the client- and server-side of Web service. In my previous article on Spring WS 2.0 and SOAP-based Web services , I explored some of the new features introduced in the 2.0 release. In this installment, I'll dive into spring-ws-test. In the previous article, we exposed a SOAP Web service endpoint (using @Endpoint annotation) on the server, but we did not write the client side. The client side can be written using Spring WS, but it is not mandatory as you can generate client-side code from WSDL using any other framework such as Axis. The advantages of using Spring WS to write client code are:

  1. Spring WS provides a lightweight alternative that does not need a WSDL contract to work. This means that you are free from any code generation (i.e. generating client code from WSDL).
  2. Spring WS requires no dependency on any other Web service framework.

Spring WS 2.0 WebServiceTemplate for Common Tasks

Spring WS provides a class called WebServiceTemplate that developers can use to perform common operations. It follows the same design principle as other Spring templates like JdbcTemplate do. The template provides convenience methods to perform the most common operations and provides a callback interface mechanism for more sophisticated use cases. For example, it provides the callback interface WebServiceMessageCallback, which lets developers perform any operation on a Web service message.

WebServiceTemplate provides methods for sending and receiving XML messages (sendAndReceive()) or request and response objects (marshalSendAndReceive()). To work with request and response objects instead of XML messages, you need to configure a marshaller for converting an object to an XML message and an unmarshaller to convert an XML response message back to a response object.

In this article, I use JAXB2 for marshalling and unmarhalling. Please make sure that you have JAXB2 JARs in your classpath. Let's begin by writing the client for the Web service we wrote in the previous article using WebServiceTemplate. The code snippet written below gets the WebServiceTemplate bean from the client application context (refer profile-service-client.xml). The WebServiceTemplate marshalSendAndReceive method takes the UserProfileCreateRequest object as a request and returns the UserProfileCreateResponse object as a response. The marshaller is configured in profile-service-client.xml using the oxm:jaxb2-marshaller tag. The oxm:jaxb2-marshaller tag resides in the http://www.springframework.org/schema/oxm namespace. You can test the client by starting the server using mvn jetty:run and then running the Client class.

import java.math.BigInteger;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.client.core.WebServiceTemplate;

import com.shekhar.usermanagement.profile.UserProfile;
import com.shekhar.usermanagement.profile.UserProfileCreateRequest;
import com.shekhar.usermanagement.profile.UserProfileCreateResponse;

public class Client {

WebServiceTemplate webServiceTemplate;

public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"com/shekhar/usermanagement/profileService/ws/client/profile-service-client.xml");
WebServiceTemplate template = applicationContext
.getBean(WebServiceTemplate.class);
Client client = new Client();
client.webServiceTemplate = template;
client.invokeProfileServiceAndGetASuccessResponse();

}

public String invokeProfileServiceAndGetASuccessResponse() {
UserProfileCreateRequest request = new UserProfileCreateRequest();
UserProfile userProfile = new UserProfile();
userProfile.setAge(BigInteger.valueOf(27));
userProfile.setFirstName("Shekhar");
userProfile.setLastName("Gulati");
request.setUserProfile(userProfile);

UserProfileCreateResponse response = (UserProfileCreateResponse) webServiceTemplate
.marshalSendAndReceive(request);
System.out.println(response.getMessage());
return response.getMessage();
}

}

The client context XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

jaxb2-marshaller id="marshaller"
contextPath="com.shekhar.usermanagement.profile" />

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
<property name="defaultUri" value="http://localhost:8080/profileService/" />
</bean>

</beans>

The client and server code can be very easily unit tested by mocking out the external dependencies using any of the mocking frameworks like EasyMock or Mockito. The problem with unit testing, however, is that it does not test the exact content of the XML message.


Tags: testing, Spring

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

Page 1 of 2



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel