我正在尝试对我的异步任务运行测试,其中一部分从数据库(房间(读取,但我收到错误
W/ContextImpl: Failed to ensure /data/user/0/MyApplication/databases: mkdir
failed: EACCES (Permission denied)
这看起来像一个运行时权限,这真的很奇怪,因为我应用程序中的相同方法在没有任何权限和任何问题的情况下运行和完成,尽管 W/ContextImpl 看起来不祥。我真的不知道这个,所以任何额外的信息只需要问,我会添加它。所以这里有一些代码,首先是测试类,在这里我实例化异步任务传递给它一个上下文并将其包装在回调中
public class ProcessJobsAsyncTaskTest {
private Context context;
@Before
public void setUp() throws Exception {
context = InstrumentationRegistry.getContext();
//have also tried InstrumentationRegistry.getTargetContext();
}
@After
public void tearDown() throws Exception {
}
@Test
public void processJobsGetJobsList() throws InterruptedException {
ProcessJobsAsyncTaskForTesting task = new ProcessJobsAsyncTaskForTesting(context);
task.setProcessJobsGetTaskListener(new ProcessJobsAsyncTaskForTesting.processJobsGetTaskListener(){
@Override
public void onComplete(JobList jobList, Exception e) {
System.out.println(Arrays.toString(jobList.getJobListsTags()));
System.out.println(jobList.getJobList());
}
}).execute();
}
}
在我的异步任务中,我获得了我的对象列表,其中包含存储库中的未来任务,如下所示:(如前所述,这适用于物理设备上的我的应用程序(
JobListRepository jobListRepository = new JobListRepository(context);
List<JobList> jobListArrayList =
jobListRepository.getFutureAllWithLimit(Constants.JOB_LISTS_TO_RETURN);
if (jobListArrayList != null){
for (JobList jobList : jobListArrayList){
Log.d(TAG,"for loop");
//get all tags and only add them if they're unique, could use a hashset
for (String tag : jobList.getJobListsTags()){
if (!tags.contains(tag)){
tags.add(tag);
}
}
listOfJobs.addAll(ListConverter.
jobListFromString(jobList.getJobList()));
}
}else{
return null;
}
在这里,我将添加完整的堆栈跟踪任何并帮助赞赏
W/ContextImpl: Failed to ensure /data/user/0/com.sealstudios.bullsheetgenerator2.test/databases: mkdir failed: EACCES (Permission denied)
E/SQLiteLog: (14) cannot open file at line 36758 of [8201f4e1c5]
E/SQLiteDatabase: Failed to open database '/data/user/0/com.sealstudios.bullsheetgenerator2.test/databases/jobLists'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:918)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:895)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:798)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:724)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
at com.sealstudios.bullsheetgenerator2.database.JobDao_Impl.getAllJobListsWithLimit(JobDao_Impl.java:321)
at com.sealstudios.bullsheetgenerator2.database.JobListRepository$getFutureJobListsWithLimit.call(JobListRepository.java:94)
at com.sealstudios.bullsheetgenerator2.database.JobListRepository$getFutureJobListsWithLimit.call(JobListRepository.java:82)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
W/System.err: java.util.concurrent.ExecutionException: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at java.util.concurrent.FutureTask.report(FutureTask.java:123)
at java.util.concurrent.FutureTask.get(FutureTask.java:193)
at com.sealstudios.bullsheetgenerator2.database.JobListRepository.getFutureAllWithLimit(JobListRepository.java:73)
at com.sealstudios.bullsheetgenerator2.jobtasks.ProcessJobsAsyncTaskForTesting.doInBackground(ProcessJobsAsyncTaskForTesting.java:50)
at com.sealstudios.bullsheetgenerator2.jobtasks.ProcessJobsAsyncTaskForTesting.doInBackground(ProcessJobsAsyncTaskForTesting.java:26)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:918)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:895)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:798)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:724)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
at com.sealstudios.bullsheetgenerator2.database.JobDao_Impl.getAllJobListsWithLimit(JobDao_Impl.java:321)
at com.sealstudios.bullsheetgenerator2.database.JobListRepository$getFutureJobListsWithLimit.call(JobListRepository.java:94)
at com.sealstudios.bullsheetgenerator2.database.JobListRepository$getFutureJobListsWithLimit.call(JobListRepository.java:82)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more
请确保通过InstrumentationRegistry.getTargetContext()
获得的context
被传递到jobLists
数据库(/data/user/0/com.sealstudios.bullsheetgenerator2.test/databases/jobLists
(
这似乎是测试可能出错的唯一原因Permission denied