| Join wMUsers | Blog at wMUsers | User Control Panel | Site Map | webMethods Jobs |For Employers |
![]() |
![]() |
The importance of unit testingUnit testing helps ensure quality in the software development process. A webMethods service makes a good candidate as a unit for testing because each is treated as a black box of business logic. Each service has an expected set of pre-conditions and post-conditions. The implementation of a unit test is simplified by using an open-source standard like JUnit. With a simple Java test client, for example, JUnit can execute services for the creation of a unit test library. JUnit can be used to test both webMethods Flow and Java services. However, this requires a utility capable of accessing webMethods services as a Java client. The Java client allows individual JUnit tests to invoke webMethods services . Furthermore, the Java client allows access to the IData pipeline so that control is granted over input and output parameters. Implementing this client is not very different than implementing any other Java client for webMethods. It makes use of the webMethods client API found in client.jar and that is distributed with the webMethods Integration Server. Note:For more information about creating Java clients, reference the webMethods Developer User's Guide. A sample JUnit Test Client Utility
package com.icf.testutils;
import com.wm.app.b2b.client.Context;
import com.wm.data.IData;
/**
* This is a WebMethods java client that can be used for
* executing services in a JUnit test.
* @author Pete Arvanitis (parvanitis@icfconsulting.com)
* @version 1.0
*
* Copyright 2003 ICF Consulting
*/
public class TestClient {
private String server = "localhost:5555";
private Context context;
private String userName = "Administrator";
private String password = "manage";
/**
* Constructs the client used for executing the JUnit test.
* @param server the address of the WebMethods server (i.e.
* localhost:5555)
* @param userName the name of the user that will be executing
* the service
* @param password the password of of the user that will be
* executing the service.
*/
public TestClient(String server,
String userName,
String password) {
this.server = server;
this.userName = userName;
this.password = password;
}
/**
* This method will invoke a service on the webmethods server.
* @param packageName the name of the package.
* @param serviceName (i.e. sampleservice)
* @param data the data that will be passed to the service as
* the input pipeline. This is in the form of an IData object
* @return and IData object that represents the output
* pipeline of the server.
* @throws Exception if an error occurs during execution. A
* simple exception is thrown so that the JUnit test can
* catch and report any exceptions that occur during the
* execution of this method.
*/
public IData invoke(String packageName,
String serviceName,
IData data)
throws Exception {
Context context = new Context();
context.connect(server, userName, password);
IData out = context.invoke(packageName, serviceName, data);
context.disconnect();
return out;
}
}
The TestClient class above establishes a connection with the webMethods Integration Server. Moreover, its invoke method executes a specific service on the server. The method receives an IData object as the input pipeline and returns an IData object as the output pipeline. The JUnit tests use the test client to execute a service and access its pipeline. Building a test client for an existing serviceThe next sample creates a Test Case that tests two built-in webMethod's services: The structure of the test assumes that the JUnit class itself is based on a package (i.e. pub.string) and that each unit test is implemented as a method of this class (testToUpper and testConcat). We first create our JUnit test class (called StringTest), it is an empty class that extends junit.framework.TestCase. StringTest contains one method, called suite, which is used by the JUnit test runner for execution.
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* This is an example of a JUnit test that will unit test
* a webMethod's service.
*/
public class StringTest extends TestCase {
/**
* The suite method for a JUnit test.
* @return
*/
public static Test suite() {
return new TestSuite(StringTest.class);
}
}
Each test is a method of this JUnit class and starts with the string "test". For example, our test for the
/**
* This test is used to test pub.string.toUpper
* @throws Exception
*/
public void testToUpper() throws Exception{
//TEST CODE GOES HERE
}
/**
* This test is used to test pub.string.concat
* @throws Exception
*/
public void testConcat() throws Exception{
//TEST CODE GOES HERE
}
The body of this method is the code that executes the service and asserts the results. First, the method creates the IData object representing the input pipeline. This involves creating a two-dimensional array representing a name-value pair. Go Deeper on the Subject: The wMUsers Discussion Forums Pete Arvanitis is a Senior Software Enginer with ICF Consulting. Pete has over 7 years of commercial development experience with emphasis in financial services, healthcare informatics, and government industries. Pete's emphasis since 1999 has been on server-side application development utilizing J2EE technologies.
Pete can be reached via email at |
| © All Rights Reserved, 2001-2008. |