Skip to content
menacher edited this page Dec 10, 2011 · 13 revisions

Test cases should be put in the src/test/java folder. They can be coded in java or Groovy.

Test Classes

Test classes should have the spring Context configuration annotation and extend the AbstractTestNGSpringContextTests class as shown below:
@ContextConfiguration( locations=("classpath:ws-int-test-beans.xml") )
class WSFuncTests extends AbstractTestNGSpringContextTests
ws-int-test-beans.xml is the spring beans file located at src/main/resources folder.

Test Methods

Test methods should return void and be annotated with @Test (org.testng.annotations.Test). Also, it is a best practice to provide a groups parameter to the annotation such that you can run targeted tests. For e.g "long-running", "test-interface-1". Note that the groups can be comma separated values and same test can belong to multiple groups.
@Test(groups="schema-test")
public void sendHolidayRequest()

How to write an end to end functional test case.

Synchronous scenario.

Take the following scenario. You are developing a web service/system that allows clients to invoke a web service on it. The client is expecting a synchronous reply. However to reply back to client, your system in turn relies on another web service. To test out this scenario, the test suite needs to act as the client as well as the server. While mocking is an option, another way would be as shown below:

  1. Use the Spring-ws WebServiceTemplate and invoke your web service1 and thereby prompt it to invoke web service2.
  2. Act as web service2 by defining a Spring-ws web service Endpoint(take a look at src/test/org.menacheri.soapsim.HumanResourceEndpoint class. This end point will receive the SOAP call from your system and publish it on an in-vm channel, you can consider it like a jms queue.
  3. A MessageCallback defined earlier will pickup this message from in-vm channel and provide a reply back to your system.
  4. At client side, check the reply from your web service and do necessary assertions.

Step 1
Groovy can be used to create simple xml templates using """ for multiline strings. In order to send a simple xml as String to the remote your service use the following method.
//client is of class org.menacheri.soapsim.WebServiceClient which is thin wrapper over the spring-ws WebServiceTemplate
String reply = client.getStringReqReply(xmlRequest,service1Url);

Step 2

    
	@PayloadRoot(namespace=NAMESPACE_URI, localPart="HolidayRequest")
	@ResponsePayload
	public Element processHolidayRequest(@RequestPayload Element holidayRequest)
	{
		System.out.println("In processHolidayRequest, thread: " + Thread.currentThread().getName());
		Element response = publish(holidayRequest);
		return response;
	}
	
	public Element publish(Element holidayRequest)
	{
		if(null == holidayRequest) return null;
		Element response = null;
		IMessage message = messageFactory.createHolidayMessage(holidayRequest);
		IMessage reply = inVMChannel.publish(message);
		if(null!=reply){
			response = (Element)reply.getPayload();
		}
		return response;
	}

Step 3
Define a MessageCallback to be executed when the message is received from your system. i.e. act as web service2.

        public IMessageCallback subscribeSyncToHolidayRequest(String status, String empId)
	{
		MessageIdentity identity = new MessageIdentityImpl(empId);
		
		MessageCallback callback = new MessageCallback(){
			@Override
			public void onCallback(IMessage message){// Message is an in-out parameter
				println "Callback thread:  ${Thread.currentThread().name}"
				// Get the xml response from the template and set it as pay load on the message.
				Element response = XMLUtils.convertStringToJdomElement(xmlResponse);
				// Set the response as the new message pay load. 
				// This will be used by the end point to reply back to client
				message.setPayload(response);
			}
		}
		// Subscribe the code on the channel. Note the "sync"
		inVMChannel.subscribeSync(callback,identity);
		return callback;
	}    

A number of things are going on here. The first being that we define an identity object, this object is what is used to lookup a callback and execute. The incoming xml at the Spring-ws Endpoint will be parsed to get this identity object

Clone this wiki locally