WebService 接口的测试方法

来源:互联网 发布:Java 鸡翁一值钱五Java 编辑:程序博客网 时间:2024/03/29 04:48
 

有以下多种方式:

一、通过WSCaller.jar工具进行测试:

前提:知道wsdl的url。

wsCaller可执行程序的发布方式为一个wsCaller.jar包,不包含Java运行环境。你可以把wsCaller.jar复制到任何安装了Java运行环境(要求安装JRE/JDK 1.3.1或更高版本)的计算机中,用以下命令运行wsCaller:

java -jar wsCaller.jar

使用wsCaller软件的方法非常简单,下面是wsCaller的主界面:

首先在WSDL Location输入框中输入你想调用或想测试的Web Service的WSDL位置,如“http://www.somesite.com/axis/services/StockQuoteService?wsdl”,然后点“Find”按钮。wsCaller就会检查你输入的URL地址,并获取Web Service的WSDL信息。如果信息获取成功,wsCaller会在Service和Operation下拉列表框中列出该位置提供的Web Service服务和服务中的所有可调用的方法。你可以在列表框中选择你要调用或测试的方法名称,选定后,wsCaller窗口中间的参数列表框就会列出该方法的所有参数,包括每个参数的名称、类型和参数值的输入框(只对[IN]或[IN, OUT]型的参数提供输入框)。你可以输入每个参数的取值。如下图:

这时,如果你想调用该方法并查看其结果的话,只要点下面的“Invoke”按钮就可以了。如果你想测试该方法的执行时间,则可以在“Invoke Times”框中指定重复调用的次数,然后再按“Invoke”按钮。wsCaller会自动调用你指定的方法,如果调用成功,wsCaller会显示结果对话框,其中包括调用该方法所花的总时间,每次调用的平均时间和该方法的返回值(包括返回值和所有输出型的参数)。如下图:

wsCaller软件是基于Axis库(Apache eXtensible Interaction System)开发的,Axis库的介绍及其版权信息请参见Apache Software Foundation的网站http://www.apache.org/。

 

二、编写java的Main()函数,创建XFireProxyFactory调用声明的Interface接口函数。

前提:知道wsdl的url、知道webservice程序的接口(方法)声明。

举例:

public static void main(String[] args) {
  Service srvcModel = new ObjectServiceFactory()
    .create(IHelloWorldService.class);
  XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
    .newInstance().getXFire());

  String helloWorldURL = "http://99.48.225.100:9999/WS/services/HelloWorldService";
  try {
   IHelloWorldService srvc = (IHelloWorldService) factory.create(
     srvcModel, helloWorldURL);
   System.out.print(srvc.example("dennis"));
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }

 }

 

三、利用Eclipse向导生成webservice的客户端调用程序。

前提:知道wsdl的url、知道webservice程序的接口(方法)声明。

步骤:

1)在Eclipse中加入Xfire插件:

在Eclipse的安装目录的plugins下包含:

org.codehaus.xfire.eclipse.ui_1.0.2.xfire126.jar、

org.codehaus.xfire.eclipse.ui.wizards_1.0.2.xfire126.jar、

org.codehaus.xfire.eclipse.core_1.0.2.xfire126.jar

在Eclipse的安装目录的features下包含:

org.codehaus.xfire.eclipse_1.0.2.xfire126

2) 在Eclipse中创建一个Web Project项目,设为WSClient项目名。

同时点击项目右键,添加Xfire nature。

3)在WSClient项目下创建一个Web Service Client文件组【或者是Code Generation from WSDL document的文件组!】。

即点击项目右键,new--》other--》...

输入webservice的url,比如:http://localhost:9999/WS/services/HelloWorldService?wsdl

再选好生成文件放置目录。完成生成!

4)修改所生成文件组中的文件:****client.java 中的main函数.

比如:生成的文件为BizRemoteServiceClient.java,在main函数中找到:

BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoapLocalEndpoint();

该语句用BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoap();代替即可!

 

public static void main(String[] args) {

  BizRemoteServiceClient client = new BizRemoteServiceClient();

  //create a default service endpoint
  //BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoapLocalEndpoint();

  BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoap();

  //TODO: Add custom client code here
  //
  //bizRemoteServiceSoap.yourServiceOperationHere();

  MyResponse MyResponse=bizRemoteServiceSoap.login("ivy1", "111");
  System.out.println(MyResponse.getFlag());
  System.out.println(MyResponse.getDetail());
  
  System.out.println("test client completed");
  System.exit(0);
 }

 

绿色代码部分是根据测试业务逻辑加入的测试代码!试具体测试任务而定!

直接运行该main函数,看结果即可!

原创粉丝点击