【问题标题】:Android testcase with database and access to TestProject's resources带有数据库和访问 TestProject 资源的 Android 测试用例
【发布时间】:2014-11-07 09:09:29
【问题描述】:

我找到了访问 TestProject 资源的方法

Accessing resources in an android test project

但是当我使用 ActivityTestCase 而不是 AndroidTestcase 时,我无法访问数据库

java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:66)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:54)
at com.j256.ormlite.dao.BaseDaoImpl.idExists(BaseDaoImpl.java:805)
at com.j256.ormlite.dao.BaseDaoImpl.createOrUpdate(BaseDaoImpl.java:335)

有没有办法两者兼得?

【问题讨论】:

    标签: android sqlite android-resources


    【解决方案1】:

    好的,我解决了我的问题:

    我现在使用 InstrumentationTestCase(我认为 ActivityTestCase 也可以)

    问题在于有两个上下文

        Context mainProjectContext = getInstrumentation().getTargetContext();
        Context testProjectContext = getInstrumentation().getContext();
    

    现在我可以从 testProjectContext 获取资源并让数据库与 mainProjectContext 一起运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多