2015-07-21 103 views
2

在編寫測試用例之前,我不知道如何編寫測試用例,當我看到在線教程時,我明白如何爲成功和失敗場景的簡單方法編寫測試用例。現在我有一個http get方法,它調用一個restful API並返回一個json響應。我有6個參數包含在url中並獲得json響應。現在,我的理解到目前爲止是成功的情況下,我應該只是硬編碼這些輸入參數,並測試如果我得到json回來和失敗沒有得到json響應回來。這是正確的還是我必須做別的?Junit測試用例,用於使用mockito的寧靜客戶端

我的意思是我有一個代碼,像

public List getStoreLocations(StoreData storeData) { 
    List storeList = null; 

    try { 
    HttpClient httpclient = HttpClientBuilder.create().build(); 
    StringBuilder urlStrngBuildr = new StringBuilder(
      https://<hostname>/xyz/abc); 
    Utility.addParameterToUrl(urlStrngBuildr, 
      Utility.APP_NAME, 
      Constants.APP_VALUE); 
    Utility.addParameterToUrl(urlStrngBuildr, 
      Constants.VERSION_PARAM_NAME, 
      Constants.VERSION_PARAM_VALUE); 
    if (storeData.getCity() != null && storeData.getState() != null) { 
     StringBuilder addressParamValue = new StringBuilder(
       storeData.getCity()); 
     addressParamValue.append(Constants.COMMA); 
     addressParamValue.append(storeData.getState()); 
     Utility.addParameterToUrl(urlStrngBuildr, 
       Constants.ADDRESS_PARAM_NAME, 
       addressParamValue.toString()); 
    } else if (storeData.getZip() != null) { 
     Utility.addParameterToUrl(urlStrngBuildr, 
       Constants.ZIP_PARAM_NAME, storeData.getZip()); 
    } 

    Utility.addParameterToUrl(urlStrngBuildr, 
      Constants.PRODUCT_PARAM_NAME, 
      storeData.getProduct()); 
    Utility.addParameterToUrl(urlStrngBuildr, 
      Constants.COUNTRY_PARAM_NAME, 
      storeData.getCountry()); 
    Utility.addParameterToUrl(urlStrngBuildr, 
      Constants.DISTANCE_PARAM_NAME, 
      storeData.getDistance()); 
    Utility.addParameterToUrl(urlStrngBuildr, 
      Constants.SIZE_PARAM_NAME, storeData.getSize()); 

    HttpGet getRequest = new HttpGet(new java.net.URI(
      urlStrngBuildr.toString())); 

    getRequest.addHeader(BasicScheme.authenticate(
      new UsernamePasswordCredentials(username,password), 
     Constants.ENCODING_TYPE, false)); 

    JSONResponseHandler responseHandler = new JSONResponseHandler(); 
    String json = httpclient.execute(getRequest, responseHandler) 
      .toString(); 

    Gson gson = new Gson(); 

    StoreResponse response = gson.fromJson(json, 
      StoreResponse.class); 

    StoreDetails[] strDetails = response.getResult(); 

    storeDetailsList = Arrays.asList(strDetails); 

    } catch (Exception exeption) { 
    exeption.printStackTrace(); 
    } 

    return storeList; 

} 

回答

1

也許你應該看看REST-assured,這是一個REST API測試框架。

好的是,它更容易閱讀,支持JSON和XML,並允許您測試HTTP代碼或響應中的特定值等內容。

get("/lotto") 
    .then() 
     .assertThat().body("lotto.lottoId", equalTo(5)); 

你可以用param方法添加參數:

given() 
    .param("key1", "value1") 
    .param("key2", "value2") 
when(). 
    aso... 

如果需要認證,就像在你的代碼,你可以使用類似以下內容:

given() 
    .auth() 
    .basic(username,password) 
    .when() 
    .get("/secured") 
    .then() 
    .statusCode(200);` 

希望這有助於您的測試。

0

它看起來像你需要模擬的主要方法是HTtpClient。那麼你如何創建一個獲取客戶端的方法,然後模擬該方法,以便它返回一個模擬HttpClient。

public HttpClient getHttpClient(){ 
    return HttpClientBuilder.create().build(); 
} 

然後在你的方法,你會做什麼:

HttpClient httpclient = getHttpClient(); 

然後在你的單元測試代碼,你會嘲笑getHttpClient方法,像這樣..

HttpClient mockClient = mock(HttpClient.class); 
MyClassBeingTested instance = spy(new MyClassBeingTested()); 
when(instance .getHttpClient()).thenReturn(mockClient); 
when(mockClient.execute(any(HttpGet.class),any(JSONResponseHandler.class)).thenReturn(testJsonString); 
List actual = instance.getStoreLocations(storeData); 

類似的東西。