我想在Android Studio中製作一些Android Instrumentation類,以便測試我的ormlite類。 ormlite的DBHelper
類需要從我的res/raw/ormlite_config.txt
中的ormlite配置文件讀取,並使用R.raw.ormlite_config
訪問。在Android Instrumentation中訪問R.raw資源jUnit測試
這不是我用openRawResource(R.raw.ormlite_config)
得到的東西,因爲DBHelper的超類的構造函數需要int資源。
當我運行我的測試,它無法雖然找到它:
android.content.res.Resources$NotFoundException: Resource ID #0x7f090001
下面是完整的堆棧跟蹤:
android.content.res.Resources$NotFoundException: Resource ID #0x7f090001
at android.content.res.Resources.getValue(Resources.java:1266)
at android.content.res.Resources.openRawResource(Resources.java:1181)
at android.content.res.Resources.openRawResource(Resources.java:1158)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.<init>(OrmLiteSqliteOpenHelper.java:76)
at com.inadaydevelopment.herdboss.DB.<init>(DB.java:40)
at com.inadaydevelopment.herdboss.DB.shared(DB.java:31)
at com.inadaydevelopment.herdboss.ORMLiteTest.setup(ORMLiteTest.java:29)
...
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
Tests ran to completion.
的DBHelper:
public class DBHelper extends OrmLiteSqliteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
}
我jUnit測試用例:
@RunWith(AndroidJUnit4.class)
public class ORMLiteTest {
@Before
public void setup() {
DB.shared(InstrumentationRegistry.getContext());
}
}
固定Commonsware的回答是:
@RunWith(AndroidJUnit4.class)
public class ORMLiteTest {
@Before
public void setup() {
DB.shared(InstrumentationRegistry.getTargetContext());
}
}