jeudi 8 novembre 2018

Testing android login method

I am developing an Android application where testing is part of development process. Testing in general is new domain for me therefore, i struggle often. I need an advice on unit testing a method which I use to perform a user login. Since method is void and implements callbacks I assume that either class or some of the objects should be mocked. In my application I use robolectric and mockito for testing purposes. I have provided the code below. Thanks in advance.

 public void loginUser(String email,String pass){
        try {
            requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
            StringRequest request = new StringRequest(Request.Method.POST, Url+"/checkUser.php", new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    if(response.contains("present")){
                        Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(getActivity(), MainActivity.class);
                        startActivity(intent);
                    }
                    else{
                        Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> parameters = new HashMap<String,String>();
                    parameters.put("email",email);
                    parameters.put("password",pass);

                    return parameters;
                }
            };
            requestQueue.add(request);
        }catch (Exception o){Log.w("TAG",o.getMessage());}
    }//loginUser

Aucun commentaire:

Enregistrer un commentaire