Question

I have a ContentProvider that is attempting to call matrixCursor.newRow(). This is crashing with a NullPointerException in private method MatrixCursor.ensureCapacity(). Debugging into this, I see that matrixCursor.data is null (appears the ShadowMatrixCursor does not instantiate it in the constructor).

I'm using the latest Robolectric jar, version 2.2.

@RunWith(RobolectricTestRunner.class)
public class MatrixCursorTest {

    @Test
    public void thisCrashesInNewRow() {
        MatrixCursor c = new MatrixCursor(new String[] { "test", "cols" }, 1);
        MatrixCursor.RowBuilder b = c.newRow();  // This crashes with NPE
    }
}

I'm trying to understand how I can get around this. I've tried creating "MyShadowMatrixCursor" as follows, but I just don't see how I can override the behavior of newRow() to just simply return an empty RowBuilder (whose constructor is default/package-private, so not accessible to my Shadow.)

import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;

@Implements(value = MatrixCursor.class, inheritImplementationMethods = true)
public class MyShadowMatrixCursor extends org.robolectric.shadows.ShadowMatrixCursor {

    @RealObject
    private MatrixCursor cursor;

    @Implementation
    public RowBuilder newRow() {
        // this causes infinite loop because shadow intercepts it again:
        return cursor.newRow(); 

        // doesn't work because RowBuilder constructor is package-private
        ...return cursor.new RowBuilder() 

        // how can i return an instance of MatrixCursor.RowBuilder instead?
    }

    @Implementation
    public void ensureCapacity(int i) {
        // Override the private ensureCapacity
        // do nothing
    }
}

So, my questions from code above:

  1. How can I return an instance of the MatrixCursor.RowBuilder?

  2. Is it possible to shadow a private method as I'm attempting with ensureCapacity() above? EDIT: YES, just make it "public" in the shadow class.

Pretty new to Robolectric, so hopefully something I'm just overlooking?

EDIT: Figured out how to override a private method by just making it public in the shadow class. However, now I'm just getting NPEs elsewhere as the state of the MatrixCursor just doesn't appear to be set up at all?

Was it helpful?

Solution

Erich suggested I try Robolectric 2.3-Snapshot build, so I did and it does indeed fix this issue. See https://groups.google.com/forum/#!topic/robolectric/I5z5N5NH4Pw.

Unrelated, but two new issues are occurring now with ContentResolver.getType() which I submitted a patch here: https://github.com/robolectric/robolectric/pull/954 and am still working on ContentProvider.openFile().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top