在編寫測試用例之前,我不知道如何編寫測試用例,當我看到在線教程時,我明白如何爲成功和失敗場景的簡單方法編寫測試用例。現在我有一個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;
}