WebService测试案例

简介:  在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。
 在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。 
   1)通过WSDL文件生成客户端调用程序 
       首先我们通过http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我们可以获得WSDL文件HelloWorldService.wsdl,并将其放在src目录下面,接着我们通过程序访问该WSDL文件,并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示: 
Java代码 
  1. package test;  
  2.   
  3. import org.codehaus.xfire.client.Client;  
  4. import org.springframework.core.io.ClassPathResource;  
  5. import org.springframework.core.io.Resource;  
  6. import webservice.HelloWorld;  
  7.   
  8. /** 
  9.  *Copyright2007GuangZhouAmigo. 
  10.  *Allrightreserved.    
  11.  *HelloWorld的webservice的测试类. 
  12.  *@author<a href="mailto:xiexingxing1121@126.com">AmigoXie</a> 
  13.  *@version1.0 
  14.  *Creationdate:2007-9-16-下午05:36:05 
  15.  */  
  16. public class WebServiceClientTest {   
  17.     HelloWorld helloWorld = null;  
  18.   
  19.     public static void main(String[] args) throws Exception {  
  20.        WebServiceClientTest test = new WebServiceClientTest();  
  21.        test.testClient();  
  22.     }   
  23.       
  24.     public void testClient() throws Exception {  
  25.        String wsdl = "HelloWorldService.wsdl"//对应的WSDL文件  
  26.        Resource resource = new ClassPathResource(wsdl);   
  27.        Client client = new Client(resource.getInputStream(), null); //根据WSDL创建客户实例  
  28.          
  29.        Object[] objArray = new Object[1];  
  30.        objArray[0] = "kk";  
  31.        //调用特定的Web Service方法  
  32.        Object[] results = client.invoke("sayHelloWorld", objArray);  
  33.        System.out.println("result: " + results[0]);  
  34.     }  
  35. }  


运行该类,可得到如下输出结果: 

result: hello,kk 

可看出运行结果正确。 

2)根据服务地址创建客户端调用程序 

     接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立WebServiceClientTest.java文件,并在src目录下建立客户端调用的Spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebService的bean,该bean访问wsdlDocumentUrl为http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。该xml文件的详细内容如下: 
Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.     <bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">  
  6.        <property name="serviceClass">            
  7.                <value>webservice.HelloWorld</value>         
  8.        </property>        
  9.         <property name="wsdlDocumentUrl">           
  10.               <value>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl</value>         
  11.         </property>        
  12.      </bean>  
  13. </beans>  


在WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示: 

Java代码 
  1. package test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import webservice.HelloWorld;  
  6.   
  7. /** 
  8.  *HelloWorld的webservice的测试类. 
  9.  */  
  10. public class WebServiceClientTest {   
  11.     HelloWorld helloWorld = null;  
  12.   
  13.     public static void main(String[] args) {  
  14.        WebServiceClientTest test = new WebServiceClientTest();  
  15.        test.testClient();  
  16.     }   
  17.   
  18.     public void testClient() {  
  19.        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  20.               "client.xml");  
  21.        helloWorld = (HelloWorld) ctx.getBean("testWebService");  
  22.        System.out.println(helloWorld.sayHelloWorld("kk"));  
  23.     }  
  24. }  


在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息: 

    hello,kk 

    由此可看出调用Web Service成功。



目录
相关文章
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
|
Java 编译器
Java注解案例-简单测试框架
Java注解案例-简单测试框架
115 0
|
12月前
|
人工智能 供应链 安全
AI辅助安全测试案例某电商-供应链平台平台安全漏洞
【11月更文挑战第13天】该案例介绍了一家电商供应链平台如何利用AI技术进行全面的安全测试,包括网络、应用和数据安全层面,发现了多个潜在漏洞,并采取了有效的修复措施,提升了平台的整体安全性。
519 4
|
前端开发 机器人 测试技术
【RF案例】Web自动化测试弹窗处理
在进行Web自动化测试时,常会遇到不同类型的弹窗,如ajax、iframe、新窗口及alert/Confirm等。这些弹窗可通过Selenium进行定位与处理。其中,ajax弹窗直接定位处理;iframe需先选中再操作;新窗口类似iframe处理;而alert/Confirm则需特殊方法应对。在Robot Framework中,需先定义并获取窗口后使用特定关键字处理。此外,还有部分div弹窗需在消失前快速定位。希望本文能帮助大家更好地处理各类弹窗。
300 6
【RF案例】Web自动化测试弹窗处理
|
分布式计算 大数据 Spark
大数据-95 Spark 集群 SparkSQL Action与Transformation操作 详细解释与测试案例(二)
大数据-95 Spark 集群 SparkSQL Action与Transformation操作 详细解释与测试案例(二)
144 1
|
存储 SQL 分布式计算
大数据-95 Spark 集群 SparkSQL Action与Transformation操作 详细解释与测试案例(一)
大数据-95 Spark 集群 SparkSQL Action与Transformation操作 详细解释与测试案例(一)
170 0
|
测试技术
软件测试高频面试题“黑盒测试之正交试验法”案例剖析与实践应用
软件测试高频面试题“黑盒测试之正交试验法”案例剖析与实践应用
713 0
|
安全 测试技术
软件测试项目式学习二(认识软件测试及软件测试分类与案例分析)
软件测试项目式学习二(认识软件测试及软件测试分类与案例分析)
269 1
|
前端开发 测试技术 数据安全/隐私保护
软件测试 —— 案例系统缺陷报告
软件测试 —— 案例系统缺陷报告
361 1