首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Robolectric中使用MatrixCursor.newRow()

无法在Robolectric中使用MatrixCursor.newRow()
EN

Stack Overflow用户
提问于 2014-02-06 05:05:05
回答 1查看 638关注 0票数 2

我有一个ContentProvider试图调用matrixCursor.newRow()。这与私有方法NullPointerException中的MatrixCursor.ensureCapacity()崩溃。通过调试,我发现matrixCursor.data为null ( ShadowMatrixCursor没有在构造函数中实例化它)。

我正在使用最新的Robolectric,版本2.2。

代码语言:javascript
复制
@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 (其构造函数是默认/包私有的,所以我的影子无法访问)。

代码语言:javascript
复制
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
    }
}

所以,我的问题来自上面的代码:

  1. 如何返回MatrixCursor.RowBuilder的实例?
  2. 在我尝试使用上面的ensureCapacity()时,有可能隐藏一个私有方法吗?编辑:是的,只需在阴影类中“公开”即可。

对罗波利克来说很新鲜,所以希望我只是忽略了一些东西?

编辑:想出如何通过在阴影类中公开私有方法来覆盖它。但是,现在我只是在其他地方获得NPEs,因为MatrixCursor的状态似乎根本没有设置?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21594443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档