I am trying to create a file in the Android JUNIT testcase setup:
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);
}
I get an exception No Such File or Directory when I call createNewFile. Is there a standard way for creating files from Android JUNIT?
In Android, directory/file creation and access are usually managed by Context. for instance, this is typically how we create directory file under application’s internal storage:
Check out the API, there are many other useful method getXXXDir() you can use for creating/accessing file.
Back to JUnit topic, suppose you use ActivityInstrumentationTestCase2 and your application project has package name
com.example, and your test project has package namecom.example.test:Hope this helps.