
| Testing, Testing, Easy as 1-2-3 with JUnit
By Pete Arvanitis | |
Next, the webMethods client API IDataFactory class is used to convert the array into an IData object. In the case of pub.string:toUpper, there is only one input parameter -- inputString.
The code to create the input pipeline looks resembles the following:
//We create our iData object from an 2 dimentional array of values.
Object [][] iDataRaw = new Object[1][2];
iDataRaw[0][0] = "inString";
iDataRaw[0][1] = "abcde";
IData inputPipeline = IDataFactory.create(iDataRaw);
After creating the input pipeline, we invoke the service using the TestClient class created earlier. This returns an IData object that represents the output pipeline.
//We create the test client and invoke the service being tested
TestClient client = new TestClient("localhost:5555",
"Administrator",
"manage");
IData outputPipeline = client.invoke("pub.string",
"toUpper",
inputPipeline);
The output pipeline is used to access the values returned by the service.
Finally, we use the assert methods of the JUnit API to validate that the output contains the values we expect.
//We get the output pipeline and test the method's value variable
//to make sure the method works.
IDataCursor opc = outputPipeline.getCursor();
String valueString = IDataUtil.getString(opc, "value");
assertEquals(valueString, "ABCDE");
The complete JUnit test method for pub.string:toUpper
Putting it all together, the complete JUnit test method for pub.string.toUpper is listed below:
/**
* This test is used to test pub.string.toUpper
* @throws Exception
*/
public void testToUpper() throws Exception{
//We create our iData object from an 2 dimentional array of values.
Object [][] iDataRaw = new Object[1][2];
iDataRaw[0][0] = "inString";
iDataRaw[0][1] = "abcde";
IData inputPipeline = IDataFactory.create(iDataRaw);
//We create the test client and invoke the service being tested
TestClient client = new TestClient("localhost:5555",
"Administrator",
"manage");
IData outputPipeline = client.invoke("pub.string",
"toUpper",
inputPipeline);
//We get the output pipeline and test the method's value variable
//to make sure the method works.
IDataCursor opc = outputPipeline.getCursor();
String valueString = IDataUtil.getString(opc, "value");
assertEquals(valueString, "ABCDE");
}
The JUnit test method for pub.string:concat
Creating the test for the concat service is very similar to the test method for pub.string:toUpper.
pub.string:concat has two input parameters (inString1 and InString2) instead of just one, though. This is handled by adding an additional dimension to the object array.
//We create our iData object from an 2 dimentional array of values.
Object [][] iDataRaw = new Object[2][2];
iDataRaw[0][0] = "inString1";
iDataRaw[0][1] = "ABC";
iDataRaw[1][0] = "inString2";
iDataRaw[1][1] = "DEF";
IData inputPipeline = IDataFactory.create(iDataRaw);
The JUnit test method for pub.string:toUpper and pub.string:concat
The following is a complete example of a complete JUnit test for pub.string:toUpper and pub.string:concat.
package com.icf.stringTest;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import com.wm.data.IData;
import com.wm.data.IDataFactory;
import com.wm.data.IDataUtil;
import com.wm.data.IDataCursor;
import com.icf.testutils.TestClient;
/**
* This is an example of a JUnit test that will unit test
* a webMethod's service.
*/
public class StringTest extends TestCase {
/**
* This test is used to test pub.string.toUpper
* @throws Exception
*/
public void testToUpper() throws Exception{
//We create our iData object from an 2 dimentional array of values.
Object [][] iDataRaw = new Object[1][2];
iDataRaw[0][0] = "inString";
iDataRaw[0][1] = "abcde";
IData inputPipeline = IDataFactory.create(iDataRaw);
//We create the test client and invoke the service being tested
TestClient client = new TestClient("localhost:5555",
"Administrator",
"manage");
IData outputPipeline = client.invoke("pub.string",
"toUpper",
inputPipeline);
//We get the output pipeline and test the method's value variable
//to make sure the method works.
IDataCursor opc = outputPipeline.getCursor();
String valueString = IDataUtil.getString(opc, "value");
assertEquals(valueString, "ABCDE");
}
/**
* This test is used to test pub.string.concat
* @throws Exception
*/
public void testConcat() throws Exception{
//We create our iData object from an 2 dimentional array of values.
Object [][] iDataRaw = new Object[2][2];
iDataRaw[0][0] = "inString1";
iDataRaw[0][1] = "ABC";
iDataRaw[1][0] = "inString2";
iDataRaw[1][1] = "DEF";
IData inputPipeline = IDataFactory.create(iDataRaw);
//We create the test client and invoke the service being tested
TestClient client = new TestClient("localhost:5555",
"Administrator",
"manage");
IData outputPipeline = client.invoke("pub.string",
"concat",
inputPipeline);
//We get the output pipeline and test the method's value variable
//to make sure the method works.
IDataCursor opc = outputPipeline.getCursor();
String valueString = IDataUtil.getString(opc, "value");
assertEquals(valueString, "ABCDEF");
}
public static Test suite() {
return new TestSuite(StringTest.class);
}
}
Use the template to test your own servies
This template provides a good starting point for building additional JUnit tests for other webMethods services. Having a complete set of unit tests is beneficial because it minimizes bugs, can be re-run for regression tests, and can be invoked by open source tools such as Grinder for load testing.
<<Prev 1 [2]
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