2011-09-30 127 views
0

我想嘲笑一個web服務調用來測試我的代碼。 以下是我想要模擬的代碼片段。 我想測試callWebService()方法。我想在調用callRestClientService(條件)時創建自己的HttpResponse。我嘗試使用JMock和EasyMock,但無法獲得所需的結果。首先,我相信我將無法模擬或創建自己的HttpResponse。嘲笑web服務

即使我不能模擬網關呼叫,我已經有一個本地服務器可以撥打到,但我必須嘲笑服務器發送的回覆來測試不同的場景。

任何人都可以幫助我這個.... 謝謝!

public class RestClientServiceResponse 
{ 
    public HttpResponse callRestClientService(final RestClientServiceCriteria criteria) throws IOException 
    { 
     final HttpUriRequest request = buildHttpUriRequest(criteria); 
     return executeRestClientServiceCall(request); 
    } 

    public HttpResponse executeRestClientServiceCall(final HttpUriRequest request) throws IOException 
    { 
     final HttpClient client = new DefaultHttpClient(); 
     final HttpResponse httpResponse = client.execute(request); 

     return httpResponse; 
    } 
} 


public class CallWebService 
{ 
    public void callWebService() 
    { 
     HttpResponse httpResponse = null; 
     try 
     { 
      httpResponse = restClient.callRestClientService(criteria); 
     } 
     catch (final Exception e) 
     { 
     System.out.println(e); 
     } 
    } 
} 
+5

你說:「我嘗試使用JMock和EasyMock,但無法獲得所需的結果」,你可以發佈你已經嘗試過嗎?給我們一些工作。 –

回答

0

如果我理解你的權利,只要使用類似嵌入式JettySimple HTTP服務器。我傾向於使用Simple,因爲它通常具有更好的文檔。你可以很容易地設置它返回任何你想要的測試。根據您所需的複雜性,您甚至可以在服務器中嵌入一個模擬器,讓您對模擬執行正常的模擬操作,將其轉換爲驗證HTTP請求並準備HTTP響應。

0

SoapUI是一個開源工具,用於測試Web服務,它支持非常好的服務模擬。請通過this tutorial瞭解如何操作。