3

I use the volley framework for my controllers in an android application One of my controllers is as below:

public class LoginApi extends AppCompatActivity {

private static final String LOGIN_URL = "example"
private static final int timeOutInMs = 10000;
private static final int numberOfTries = 1;

public LoginApi() {
}

public void doLogin(final Context context, JSONObject jsonObject) {

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, jsonObject, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            User user=new User();
            try {
                user.setAccessToken(response.getString("access_token"));
                user.setExpireToken(response.getString("expires_in"));
                user.setRefreshToken(response.getString("refresh_token"));
                user.setTokenType(response.getString("token_type"));

                Intent intent=new Intent(context,MenuCustomer.class);
                Gson myGson=new Gson();
                String myJson = myGson.toJson(user);
                intent.putExtra("myjson", myJson);
                context.startActivity(intent);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.d("", "");
            Toast.makeText(context, "Successfull login", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("", "");
            Toast.makeText(context, "Please enter a valid email and password", Toast.LENGTH_SHORT).show();

        }
    }) {
        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> map = new HashMap<>();
            map.put("Accept", "application/json");
            map.put("Content-Type", "application/json");
            return map;
        }

    };
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(timeOutInMs, numberOfTries, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    Singleton.getmInstance(context).addToRequestQueue(jsonObjectRequest);

}

}

I have tried to make mock classes (FakeHttpStack,FakeRequestQueue) and I tried to make tests from mock volley classes from here

I cant find a solution to unit test my class.

Metin Atalay
  • 1,375
  • 18
  • 28

0 Answers0