mercredi 27 avril 2016

Unit Testing Activity/Adaper which uses Picasso

I'm trying to unit test my adapter where I'm using picasso to the load images. To unittest the adapter I need mock the picasso. So that it don't load the actual images from the network.

I found one SO question. But it seems quite outdated. I'm using android testing support library.

MyAdapter.java

public class MyAdapter extends ArrayAdapter<T> {


    public PackageAdapter(Context context, ArrayList<T> data) {
        super(context, 0, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Model model = getItem(position);
        ViewHolder vh;

        if (convertView == null) {
            vh = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_list, parent, false);

            vh.imageItem = (ImageView) convertView.findViewById(R.id.iv_item_image);
            vh.textViewItem = (TextView) convertView.findViewById(R.id.tv_item_name);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        vh.textViewItem.setText(model.getName());
        Picasso.with(context)
             .load(model.getImagePath())
             .into(vh.imageItem);

        return convertView;
    }

    public static class ViewHolder {
        ImageView imageItem;
        TextView textViewItem;
    }
}

Aucun commentaire:

Enregistrer un commentaire