Web Services的测试模型与代码摘录
http://tech.ddvip.com 2008年01月18日 社区交流
内容摘要:Web Services组件可由多个利益相关者来共同构建和部署。因此,测试这些组件过程中会发现确定代码质量、可用性等都有很大的难度。Web Services的标准是简单的,数据驱动的,并且共享一个公共的基于XML的基础。
代码摘录:.NET Web Services
上面摘录的一段代码是Stock Trade Web Services,它是前面所设计的WLS Web Services的.NET版。它用与JAVA非常相似的C#语言编写。文件名是“StockTrade.asmx.cs”。
代码编写完成后,在WebLogic服务器上部署WLS客户程序,在.NET框架上部署.NET Web Services,验证客户端是否能调用Web Services。
.NET客户端和WLS Web 服务器端
·创建一个WLS Web Services(EAR文件)
·使用WLS Web 服务WSDL并通过运行wsdl.exe生成一个.NetClient Stub。这一过程将生成一个.cs文件,该文件包含对应于WSDL提供的Web Services的方法定义。
·另外一步是在Stub中定义一个新的构造函数,并将WSL URL作为一个参数。这一步是必须的,因为wsdl.exe所生成的Stub在缺省情况下指向USDL URL中的本地主机。
·创建一个.NetClient 类(另外一个.cs文件),该类实际上以WSDL URL作为构造函数参数。该类是一个代理类,它含有与Stub中所定义的相同的方法。.Net客户首先对Stub类进行了实例化,然后将方法的调用委托到Stub。
.NET Server
.Net Client
WebLogic Server
WLS Web Service
using System;
namespace interop
{
/// <summary>
/// Client for StockTrade web service
/// </summary>
public class StockTradeClient
{
public StockTradeClient()
{
//
// TODO: Add constructor logic here
//
}
static void Main()
{
string action = "BUY";
string symbol = "BEAS";
int quantity = 100;
StockTradeService stService = new StockTradeService();
Console.WriteLine("Stock Trade Service: ");
bool result = stService.execute(action,symbol, quantity);
Console.Write("Result of Stock Trade: ");
Console.WriteLine(result.ToString());
}
}
}
代码摘录:.NET客户端
上面摘录的的一段代码是对应于我们前面创建的Stock Trade Web Services的.NET客户端代码,该段代码是用与JAVA语言极为相似的C#语言开发的。文件名是“StockTradeClient.cs”。
代码编写完成后,在.NET框架上部署.NET客户端,并且在WebLogic Server上部署WLS Web Services,验证客户端是否能正确调用Web Services。
下面的就是在WebLogic Server中显示的日志的格式:
SELLING quantity: 300 of symbol:IBM
execute() in webservices.stock.trade webservice has been invoked with following
arguments:: action:SELL123 symbol:IBM quantity:300
INVALID action: SELL123
execute() in webservices.stock.trade webservice has been invoked with following
arguments:: action:SELL symbol:IBM quantity:300
SELLING quantity: 300 of symbol:IBM
execute() in webservices.stock.trade webservice has been invoked with following
arguments:: action:SELL123 symbol:IBM quantity:300
INVALID action: SELL123
责编:豆豆技术应用