我正在尝试对异步任务进行测试,它的一部分从数据库(Room)中读取,但出现错误
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;
}
在这里,我将添加完整的stacktrace并感谢您的帮助
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
答案 0 :(得分:1)
请确保将通过context
获得的InstrumentationRegistry.getTargetContext()
传递到jobLists
数据库(/data/user/0/com.sealstudios.bullsheetgenerator2.test/databases/jobLists
)
这似乎是测试可能出现Permission denied
错误的唯一原因