服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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"});

    }

}

 



trackback: http://tb.blog.csdn.net/trackback.aspx?postid=674611

扫描关注微信公众号