Junit注解方式测试Web服务

import static org.junit.Assert.*;  
  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.util.HashMap;  
import java.util.Map;  
  
  
  
import org.codehaus.xfire.client.Client;  
  
import org.junit.BeforeClass;  
import org.junit.Test;  
  
public class  Sumtest {  
    //Web服务地址  
    private final static String WS_URL="http://localhost:8080/WS/services/SumService?wsdl";  
    //创建Client  
    private static Client client=null;  
    //需要测试方法的Map  
    private static Map mothods= null;  
      
    @BeforeClass//所有测试方法前只执行一次要和@Before区分每次执行测试方法都要执行一次  
    /** 
     * 初始化方法 
     */  
    public static void setUp() throws Exception{  
        createWSClient();  
        createTestMethods();  
    }  
    //Web服务方法接口  
    private final static String WS_MOTHODS_SUM = null;  
    /** 
     * 添加Map 
     */  
    private static void createTestMethods() {  
        mothods = new HashMap();  
        mothods.put(WS_MOTHODS_SUM, "sum");  
    }  
    /** 
     * 初始化client 
     * @throws Exception 
     * @throws MalformedURLException 
     */  
    private static void createWSClient() throws Exception,  
            MalformedURLException {  
        client = new Client(new URL(WS_URL));  
    }  
      
    @Test  
    /** 
     * 测试方法 
     */  
   public void testSum() throws Exception{  
  
        Object[] obj = client.invoke(mothods.get(WS_MOTHODS_SUM),new Object[]{5});  
        Integer result = (Integer) obj[0];  
        assertEquals("与期望值不一致", new Integer(10),result);  
        System.out.println("返回调用结果:result="+result);  
    }

你可能感兴趣的:(Junit注解方式测试Web服务)