Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
private void setCountryCodeData() {
       //getting countryCode
       try {

           mCountryCode = PreferenceManager.getPrefIsoCode(LoginActivity.this);

           if (mCountryCode != null || !mCountryCode.equalsIgnoreCase("")) {
               for (CountryCodesResponse codesResponse : mCountryCodeResponse) {
                   if (codesResponse.getIso_code().equalsIgnoreCase(mCountryCode)) {
                       countrycode = codesResponse.getInternationalCallingCode();
                       countryflag = codesResponse.getImage();
                       isonumber = codesResponse.getIso_code();
                       countryname = codesResponse.getName();
                   }
               }
           } else if (getCountryCode() != null && !getCountryCode().equalsIgnoreCase("error")) {
               mCountryCode = getCountryCode();
               for (CountryCodesResponse codesResponse : mCountryCodeResponse) {
                   if (codesResponse.getIso_code().equalsIgnoreCase(mCountryCode)) {
                       countrycode = codesResponse.getInternationalCallingCode();
                       countryflag = codesResponse.getImage();
                       isonumber = codesResponse.getIso_code();
                       countryname = codesResponse.getName();
                   }
               }
           } else {
               countrycode = "+91";
               countryflag = "https://isprava-oi-development.s3.amazonaws.com/country/india/IN_3x.png";
               isonumber = "IN";
               countryname = getResources().getResourceName(R.string.india);
           }

           Glide.with(LoginActivity.this).asBitmap().load(countryflag).into(binding.tvFlagImage);

           binding.tvCountryCode.setText(countrycode);

       } catch (Exception ignored) {
           Log.e("country_code_error", "" + ignored);
           countrycode = "+91";
           countryflag = "https://isprava-oi-development.s3.amazonaws.com/country/india/IN_3x.png";
           isonumber = "IN";
           countryname = getResources().getResourceName(R.string.india);
           binding.tvCountryCode.setText(getResources().getString(R.string.india_country_code));
           binding.tvFlagImage.setImageResource(R.drawable.flag_india);
       }

       //pick mobile number from device
       requestHint();
   }


What I have tried:

I have tried it using as a mockito and then returning the value but by doing this im not able to get the code coverage. and im really confused how to write test cases for this method.
Posted
Updated 15-Dec-22 3:02am

1 solution

You really can't write proper unit tests for this. Unit testing controls the input to a method and evaluates its output. The method you posted takes no direct input and returns nothing.

If you're going to unit test code, you have to consider that in the design of the code. You're going to have to start reading up on writing testable code. how to write testable code java - Google Search[^]
 
Share this answer
 
Comments
[no name] 19-Dec-22 5:00am    
Then can we write a test case for method like this
@Override
public void onCountryCodeClicked(CountryCodesResponse codesResponse, int position) {

try {
if (codesResponse != null && !codesResponse.getInternationalCallingCode().equalsIgnoreCase("")
&& codesResponse.getImage() != null && !codesResponse.getImage().equalsIgnoreCase("")) {
binding.tvCountryCode.setText(codesResponse.getInternationalCallingCode());
Glide.with(LoginActivity.this).asBitmap().load(codesResponse.getImage()).into(binding.tvFlagImage);

} else {
binding.tvCountryCode.setText(getResources().getString(R.string.india_country_code));
binding.tvFlagImage.setImageResource(R.drawable.flag_india);
}

countrycode = codesResponse.getInternationalCallingCode();
countryflag = codesResponse.getImage();
isonumber = codesResponse.getIso_code();
countryname = codesResponse.getName();

} catch (Exception ignored) {
Log.e("country_code_error", "" + ignored);
binding.tvCountryCode.setText(getResources().getString(R.string.india_country_code));
binding.tvFlagImage.setImageResource(R.drawable.flag_india);

countrycode = "+91";
countryflag = "https://isprava-oi-development.s3.amazonaws.com/country/india/IN_3x.png";
isonumber = "IN";
countryname = getResources().getResourceName(R.string.india);

}

binding.layUpdateAvailable.setVisibility(View.GONE);
}
Dave Kreskowiak 19-Dec-22 8:09am    
No, because your method still has no output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900