package ch09;
import org.codehaus.xfire.XFire;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.aegis.AbstractXFireAegisTest;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.ServiceFactory;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import org.codehaus.xfire.service.invoker.ObjectInvoker;
import ch09.server.IweekWork;import ch09.server.weekWorkImpl;/* * 利用Junit 单元测试 */public class weekWorkText extends AbstractXFireAegisTest{public void setUp() throws Exception { super.setUp(); //使用XFire获得web服务信息,以登录web服务 XFire xFire=getXFire(); ServiceFactory factory=new ObjectServiceFactory(xFire.getTransportManager(),null); Service service=factory.create(IweekWork.class); service.setProperty(ObjectInvoker.SERVICE_IMPL_CLASS, weekWorkImpl.class);//得到接口实现类 xFire.getServiceRegistry().register(service);}//客户端测试public void testIweekWord() throws Exception{ //创建元数据 Service serviceModel=new ObjectServiceFactory().create(IweekWork.class); //创建web服务的代理 XFire xFire=XFireFactory.newInstance().getXFire(); XFireProxyFactory factory=new XFireProxyFactory(xFire); //路径 String serviceURL="http://localhost:8080/restranthgk/services/weekWork"; IweekWork iClient=null; String serviceResponse=""; try { //生成天气预报web服务调用对象 //(强制把object类型转换为接口类型)通过代理工厂factory.create(元数据,服务URL) iClient=(IweekWork)factory.create(serviceModel,serviceURL); //获得0-15的随机整数 serviceResponse=iClient.assignWork(2);//2通过接口调用方法获得当日工作安排 assertNotNull("text fail: serviceResponse is null", serviceResponse); //参数1:期望与实际不相等的信息 参数2:期望值 参数3:实际值 // assertEquals("期望值与实际值不符合", ThisERROR"准备会见火星特使!", serviceResponse); assertEquals("期望值与实际值不符合", "准备会见火星特使!", serviceResponse); } catch (Exception e) { System.out.print("测试出错:"+e.toString()); } System.out.println("测试结果:"+serviceResponse);//控制台输出结果}}