XFire Web服务的单元测试
作者: 文章来源:
发布日期:2007年04月09日 浏览次数:77次
你不必发布到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文档是否和预期一致。
更详细的内容可以查看AbstractXFireTest的API。
在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"});
- }
- }