0

I am writing test cases for RecyclerView items using Robolectric. Facing NullPointerException while calling recyclerView.measure(0,0) because RecyclerView have applied with RecyclerView.ItemDecoration and it is working fine for the RecyclerView which is not being with RecyclerView.ItemDecoration.

Exception Log:

java.lang.NullPointerException
    at ui.adapters.DividerItemDecoration.getItemOffsets(DividerItemDecoration.java:119)
    at android.support.v7.widget.RecyclerView$ItemDecoration.getItemOffsets(RecyclerView.java:8662)

Placed comment in line number 119 of DividerItemDecoration.java, and here, mDivider became NULL which is initialized from constructor.

@Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());//Line:119
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }

Constructor

public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

Where ATTRS

private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

Please guide me on how to write test cases for RecyclerView which is being with RecyclerView.ItemDecoration, thanks in advance.

SOLUTION: Finally, I resolved this issue by making Shadow class for RecyclerView as mentioned in below code and followed basic shadow mapping logic,

RecyclerViewShadow.java

@Implements(RecyclerView.class)
public class RecyclerViewShadow extends ShadowViewGroup {
    @Implementation
    public void addItemDecoration(RecyclerView.ItemDecoration decor, int index) {

    }
}
Community
  • 1
  • 1
SathishKumar
  • 1,644
  • 1
  • 14
  • 28

0 Answers0