Web Services组件可由多个利益相关者来共同构建和部署。因此,测试这些组件过程中会发现确定代码质量、可用性等都有很大的难度。Web Services的标准是简单的,数据驱动的,并且共享一个公共的基于xml的基础。传统的测试工具可能不足以有效地测试这些标准。而且GUI自动化工具也不足以有效地测试Web Services的接口点和消息格式。
System.out.PRintln("execute() in webservices.stock.trade webservice has been invoked
with following arguments:: action:" + action +
" symbol:" + symbol + " quantity:" + quantity);
if(action == null) {
throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),
"action parameter is null.",
null,
detail);
}
if(symbol == null) {
throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),
"symbol parameter is null.",
null,
detail);
}
if(action.equalsIgnoreCase("BUY"))
System.out.println("BUYING quantity: "+ quantity + " of symbol:" + symbol);
// Invoke method to execute trade here.
else if(action.equalsIgnoreCase("SELL"))
System.out.println("SELLING quantity: "+ quantity + " of symbol:" + symbol);
// Invoke method to execute trade here.
else
{
System.out.println("INVALID action: "+ action);
throw new SOAPFaultException(new QName( "http://StockTrade/execute", "ServerFailed" ),
"Invalid Action:" + action,
null,
detail);
}
return true;
}
代码摘录:Stock Trade Web Services 该段摘录的代码是Stock Trade Web Services的“execute()”方法的实现代码。该方法首先验证输入参数的有效性,验证成功才执行功能。举例说明,假如参数action是空值,它就会抛出一个SoapFaultException异常,用faultstring参数(第二个参数)说明造成异常的原因。为了举例说明,在对参数symbol进行相似的验证之后,Web Services给出了处理机。在实际的情况下,商业逻辑应该在此位置中实现: