我有一个ContentProvider试图调用matrixCursor.newRow()。这与私有方法NullPointerException中的MatrixCursor.ensureCapacity()崩溃。通过调试,我发现matrixCursor.data为null ( ShadowMatrixCursor没有在构造函数中实例化它)。
我正在使用最新的Robolectric,版本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
}
}我在试着弄清楚我怎么才能避开这一切。我尝试了如下创建"MyShadowMatrixCursor“,但我不知道如何才能覆盖newRow()的行为,只返回一个空的RowBuilder (其构造函数是默认/包私有的,所以我的影子无法访问)。
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
}
}所以,我的问题来自上面的代码:
对罗波利克来说很新鲜,所以希望我只是忽略了一些东西?
编辑:想出如何通过在阴影类中公开私有方法来覆盖它。但是,现在我只是在其他地方获得NPEs,因为MatrixCursor的状态似乎根本没有设置?
发布于 2014-02-08 08:11:11
Erich建议我尝试Robolector2.3-快照构建,所以我做到了,它确实解决了这个问题。见https://groups.google.com/forum/#!topic/robolectric/I5z5N5NH4Pw。
与此无关,但ContentResolver.getType()现在出现了两个新问题,我在这里提交了一个修补程序:https://github.com/robolectric/robolectric/pull/954,目前仍在开发ContentProvider.openFile()。
https://stackoverflow.com/questions/21594443
复制相似问题