Android Volley框架如何进行单元测试

时间:2017-11-28 10:37:35

标签: android unit-testing android-volley

我在Android应用程序中使用控制器的volley框架 我的一个控制器如下:

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);

}

}

我试图制作模拟类(FakeHttpStack,FakeRequestQueue),我试图从here

的模拟排球类进行测试

我找不到单元测试我的课程的解决方案。

0 个答案:

没有答案