ssk*_*ssk 3 junit android file
我正在尝试在Android JUNIT测试用例设置中创建一个文件:
protected void setUp() throws Exception {
super.setUp();
File storageDirectory = new File("testDir");
storageDirectory.mkdir();
File storageFile = new File(storageDirectory.getAbsolutePath()
+ "/test.log");
if (!storageFile.exists()) {
storageFile.createNewFile();
}
mContext = new InternalStorageMockContext(storageFile);
mContextWrapper = new ContextWrapper(mContext);
}
Run Code Online (Sandbox Code Playgroud)
当我调用createNewFile时,我得到一个异常No Such File或Directory.是否有从Android JUNIT创建文件的标准方法?
在Android中,目录/文件的创建和访问通常由Context管理.例如,这通常是我们在应用程序的内部存储下创建目录文件的方式:
File testDir = Context.getDir("testDir", Context.MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)
查看API,还有许多其他有用的方法getXXXDir()可用于创建/访问文件.
回到JUnit主题,假设您使用ActivityInstrumentationTestCase2并且您的应用程序项目具有包名称com.example,并且您的测试项目具有包名称com.example.test:
// this will create app_tmp1 directoryunder data/data/com.example.test/,
// in another word, use test app's internal storage.
this.getInstrumentation().getContext().getDir("tmp1", Context.MODE_PRIVATE);
// this will create app_tmp2 directory under data/data/com.example/,
// in another word use app's internal storage.
this.getInstrumentation().getTargetContext().getDir("tmp2", Context.MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
2532 次 |
| 最近记录: |