Volley和Robolectric 3.0

时间:2016-07-07 07:53:11

标签: android android-volley robolectric

我想问一下如何使用Robolectric 3.0测试Volley库是否有任何好的教程。例如,我有来自开放天气api的请求,需要测试它。

public class TodayForecast extends Fragment {

public static String URL= "http://api.openweathermap.org/data/2.5/weather?";

public static String BASE_URL= "";


String IMG_URL = "http://api.openweathermap.org/img/w/";

TextView cityText;
ImageView imageView;
private String cityName;

private String speed;
TextView countryTextView;
TextView tempTextView;
TextView windTextView;
TextView pressureTextView;
TextView humidityTextView;
TextView sunriseTextView;
TextView sunSetTextView;

private String icon;
private String retrievedLat;
private String retrievedLog;
private RequestObjects requestObjects;



public TodayForecast() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_today_forecast, container, false);

    retrievedLat = getArguments().getString("lat");
    retrievedLog = getArguments().getString("log");

    BASE_URL = URL +"lat="+retrievedLat+"&lon="+retrievedLog+"&units=metric&appid=d48708e1e4d8e2b60da14778acd8d56a";
    cityText = (TextView)rootView.findViewById(R.id.cityText);
    imageView = (ImageView) rootView.findViewById(R.id.thumbnailIcon);
    windTextView = (TextView) rootView.findViewById(R.id.windTextValue);
    tempTextView = (TextView)rootView.findViewById(R.id.tempText);
    pressureTextView = (TextView)rootView.findViewById(R.id.pressureTextValue);
    humidityTextView = (TextView)rootView.findViewById(R.id.humidTextValue);
    sunriseTextView = (TextView)rootView.findViewById(R.id.riseTextValue);
    sunSetTextView = (TextView)rootView.findViewById(R.id.setTextValue);
    countryTextView = (TextView)rootView.findViewById(R.id.countryText);
    //http://api.openweathermap.org/data/2.5/weather?lat=39.6304951&lon=22.4206619&appid=d48708e1e4d8e2b60da14778acd8d56a

    if(savedInstanceState!=null) {
        String city_name = savedInstanceState.getString("CityName");
        String city_temp = savedInstanceState.getString("CityTemp");
        String city_pressure = savedInstanceState.getString("CityPressure");
        String city_humidity = savedInstanceState.getString("CityHumidity");
        String wind_speed = savedInstanceState.getString("WindSpeed");
        String weather_icon = savedInstanceState.getString("weatherIcon");
        String sunrise_time = savedInstanceState.getString("sunrise");
        String sunset_time = savedInstanceState.getString("sunset");
        String country_name = savedInstanceState.getString("country");

        retrievedLat = savedInstanceState.getString("lat");
        retrievedLog = savedInstanceState.getString("log");


        cityText.setText(city_name);
        tempTextView.setText(city_temp + ""+"\u00b0 \u0043");
        pressureTextView.setText(city_pressure + " hPa");
        humidityTextView.setText(city_humidity +" %");
        windTextView.setText(wind_speed+ " km/h");

        Picasso.with(getActivity()).load(weather_icon).into(imageView);

        sunriseTextView.setText(sunrise_time);
        sunSetTextView.setText(sunset_time);

        countryTextView.setText(country_name);


    }else{
         weatherData();
    }
    return rootView;
}

private void weatherData() {
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            BASE_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());
                    try {
                        requestObjects = new RequestObjects();

                        requestObjects.getCityName(response,cityText);

                        JSONObject main = response.getJSONObject("main");
                        requestObjects.getMainObject(main,tempTextView,humidityTextView,pressureTextView);

                        JSONObject windValue = response.getJSONObject("wind");
                        requestObjects.getWindObject(windValue,windTextView);

                        JSONObject sysJSONObject = response.getJSONObject("sys");
                        requestObjects.getSysObject(sysJSONObject,sunriseTextView,sunSetTextView,countryTextView);
                        //getSysObject(sysJSONObject);

                        JSONArray weather = response.getJSONArray("weather");
                        requestObjects.getIcon(weather,imageView);
                    } catch (JSONException e) {
                        Log.e("TAG", e.toString());
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}




@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putString("CityName",RequestObjects.cityName);
    outState.putString("CityTemp",RequestObjects.temperature);
    outState.putString("CityPressure",RequestObjects.pressure);
    outState.putString("CityHumidity",RequestObjects.humidity);
    outState.putString("WindSpeed",RequestObjects.speed);
    outState.putString("sunrise",RequestObjects.time);
    outState.putString("sunset",RequestObjects.setTime);
    outState.putString("weatherIcon",requestObjects.imageIcon);
    outState.putString("country",requestObjects.countryName);


  }
//If onDestroy() method is called then the fragment is not stored and the webservice will run
// again.
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.v("onDestroy","onDestroy called");
  }

}

我上面的代码,我只是得到温度,湿度和日出/日落时间等信息。

由于

0 个答案:

没有答案