Hi I really hope you can help me,i want to test this class that have one public method and private methods inside it , how to test these with mockito and junit4 android. one class have one public method sendHttpRequest this method take many parameter to decide which function to excute these functions are private that i want to test 1-Test a default header was replaced by a custom one 2-Test Auth headers are not present 3-Test Auth headers are present 4-Test baseUrl is appended when isRelativeUrl is Tru 5-Test baseUrl is not appended when isRelativeUrl is False
private static String TAG = HttpService.class.getSimpleName();
public static synchronized void sendHttpRequest(
HttpMethod httpMethod,
String url,
boolean isRelativeUrl,
Map<String, String> params,
boolean isAuth,
Map<String, String> customHeaders,
JSONObject body, CallListener callListener) {
// 1- Handle relative url
String requestUrl=url;
if (isRelativeUrl) {
requestUrl = Constants.BASE_URL + url;
}
/* 2- Handle query parameters
https://dev-api.24messenger.com/mobile/oauth/token?link=emaillkfjg888&key1=val1&key2=val2
Append query parameters to requestUrl*/
if (params != null && !params.isEmpty()) {
// urlEncodeUTF8 response "?p3=a+%26+b&p2=cat&p1=12"
requestUrl = requestUrl + urlEncodeUTF8(params);
}
/*3- Handle isAuth and headers
Map<String, String> defaultHeaders
customHeaders
Merge<defaultHeaders, customHeaders)
if i want to oveRide token*/
Map<String, String> headers = new HashMap<>();
headers.putAll(Constants.defaultHeaders);
if (isAuth) {
headers.put("Access-Token", "actual token from pref");
}
headers.put("Language", "from app setting");
headers.put("Local", DeviceLocalHelper.getLocalLanguage());
headers.put("ip", CurrentIP.getIPAddress());
headers.put("OS", "android");
headers.put("Device", PhoneDataHelper.getDeviceName());
headers.put("OS-Version", PhoneDataHelper.getAndroidVersion());
if (customHeaders != null) {
for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
headers.put(entry.getKey(), entry.getValue());
}
}
switch (httpMethod) {
case GET:
getRequest(requestUrl, headers, callListener);
break;
case POST:
postRequest(requestUrl, headers, body, callListener);
break;
case PUT:
putRequest(requestUrl, headers, body, callListener);
break;
case PATCH:
patchRequest(requestUrl, headers, body, callListener);
break;
case DELETE:
deleteRequest(requestUrl, headers, callListener);
break;
}
}
private static void getRequest(String requestUrl, Map<String, String> headers, CallListener callListener) {
AndroidNetworking.get(requestUrl)
.addHeaders(headers)
.doNotCacheResponse()
.getResponseOnlyFromNetwork()
.setPriority(Priority.LOW)
.build()
.getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {
@Override
public void onResponse(Response okHttpResponse, String response) {
if (okHttpResponse != null && okHttpResponse.isSuccessful()) {
Log.d(TAG, "Headers :" + okHttpResponse.headers());
callListener.onSuccess(response);
}
}
@Override
public void onError(ANError error) {
if (error.getErrorCode() != 0 && error != null) {
Log.d(TAG, "onError errorCode : " + error.getErrorCode());
Log.d(TAG, "onError errorBody : " + error.getErrorBody());
Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
String fullError = error.getErrorCode() + error.getErrorBody() + error.getErrorDetail();
callListener.onFailure(fullError);
} else {
if (error != null) {
Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
callListener.onFailure(error.getErrorDetail());
}
}
}
});
}
Aucun commentaire:
Enregistrer un commentaire