用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
smallnest的公告
<!--// Customized Script Code //--> <SCRIPT language="javascript"> <!--// var obj= document.getElementById("blogsearchsty"); if (obj) { obj.style.display = "none"; } obj= document.getElementById("personalnav"); if (obj) { obj.style.display = "none"; } obj= document.getElementById("Header1_HeaderTitle"); if (obj) { obj.href="http://www.colobu.com"; } //--> </SCRIPT> <a title="我的站点" href="http://www.colobu.com/">官方网站(<b>www.colobu.com</b>)</a> <h3 class="listtitle"></h3>
文章分类
存档

原创  [webservices开发]XFire Web服务的单元测试 收藏

你不必发布到tomcat等容器中就可以进行测试,常用的测试类常继承AbstractXFireSpringTest 抽象类。AbstractXFireSpringTest 类又实现了AbstractXFireTest 类,AbstractXFireTest 类是TestCase的子类,所以你可以使用junit进行单元测试。

 

下面看一个例子:

package com.kuaff.xfire.samples;

 

import java.net.URL;

 

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;

import org.codehaus.xfire.client.Client;

import org.codehaus.xfire.service.Endpoint;

import org.codehaus.xfire.spring.AbstractXFireSpringTest;

import org.jdom.Document;

import org.springframework.context.ApplicationContext;

 

public class MathClientTest extends AbstractXFireSpringTest

{

    public void testService() throws Exception

    {

        Document wsdl = getWSDLDocument("MathService");

        printNode(wsdl);

    }

   

       

    protected ApplicationContext createContext()

    {

        return new ClassPathXmlApplicationContext(new String[] {

           "/org/codehaus/xfire/spring/xfire.xml",

           "/META-INF/xfire/services.xml"

        });

    }

}

 

 

你必须实现createContext方法,这个方法将得到应用上下文。在这个例子中返回了一个ClassPathXmlApplicationContext上下文,它通过类路径的下配置文件生成这个上下文对象。

剩下的工具就是写单元测试的方法,和junit的方式一样。getWSDLDocument方法可以获取指定Web服务的WSDL文档。如果你要测试发布的服务,还可以通过

invokeService方法测试:

Document response = invokeService("MathService", "Add.xml");

 

        addNamespace("k", "http://www.kuaff.com/xfire/mathservice");

        assertValid("//soap:Body/k:addResponse", response);

        printNode(response);

 

通过assertValid方法可以通过Xpath查询的方式验证返回的xml文档是否和预期一致。

更详细的内容可以查看AbstractXFireTestAPI

 

SpringSide这个开源项目中做了一个Web服务,它的单元测试代码如下:

package org.springside.bookstore.service;

import … …

 

public class XFireTest extends AbstractXFireSpringTest {

 

    protected final Log log = LogFactory.getLog(getClass());

 

    public void setUp() throws Exception {

        super.setUp();

    }

 

    public void testGetWsdl() throws Exception {

        Document doc = getWSDLDocument("BookService");

        assertValid("//xsd:complexType[@name=\"Book\"]", doc);

        assertValid("//xsd:complexType[@name=\"Category\"]", doc);

    }

 

    protected ApplicationContext createContext() {

        return new ClassPathXmlApplicationContext(new String[]{"classpath*:applicationContext*.xml"});

    }

}

 

发表于 @ 2006年04月24日 10:15:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:[webservices开发]使用xfire开发jsr181风格的web服务 | 新一篇:[webservices开发]集成Spring

  • 发表评论
  • 评论内容:
  •  
Copyright © smallnest
Powered by CSDN Blog