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:
How can I return an instance of the MatrixCursor.RowBuilder?
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?