Unit Testing Android Utility Functions
<p>So, I am relatively new to Android and am trying to sort out how to do decent unit testing of my code.  I am trying to write a unit test for a simple utility function that copies files from the assets over to the internal file system,</p>

<pre><code>public class FileUtils {
    private static String LOG_TAG = "FileUtils";
    public static void copyAssetToInternalStorage(Context ctxt, final String assetName, final String filePath) 
        throws IOException {
...
        File filesDir = ctxt.getFilesDir();
        File outFile = new File(filesDir, filePath);
        Log.d(LOG_TAG, "Internal File Path: " + outFile.getAbsolutePath());
...
    }
}
</code></pre>

<p>In my test project I have the test,</p>

<pre><code>public class FileUtilsTest 
    extends InstrumentationTestCase {

    public void testCopyAsset() {
        String ASSET_FILE = "TestFile_1.txt";
            String DEST_FILE_1 = "DestFile_1.txt";

        Context ctxt = this.getInstrumentation().getContext();

        try {
            FileUtils.copyAssetToInternalStorage(ctxt, ASSET_FILE, DEST_FILE_1);

            File filesDir = ctxt.getFilesDir();
            File tstFile1 = new File(filesDir, DEST_FILE_1);

            Assert.assertTrue("Output file does not exist", tstFile1.exists());
            Assert.assertEquals("Output file is not the correct size", 
                    154, tstFile1.length());

        } catch (IOException ex) {
            ex.printStackTrace();
            Assert.fail("Exception: " + ex.getClass() + ": " + ex.getMessage());
        }
    }
}
</code></pre>

<p>I have the asset used as the input for the test in the assets directory in the test project, hence the use of the this.getInstrumentation().getContext() for the context I am working under for the test.</p>

<p>When I run the test, if finds the asset just fine, but when the function tries to write to the internal file system the call to ctxt.getFilesDir() is logging an error.  What I see using "adb logcat" is this,</p>

<pre><code>application com.perigee.brewing.recipeeditor: pid=803 uid=10037 gids={}
I/TestRunner(  803): started: testCopyAsset(com.perigee.utils.FileUtilsTest)
W/ApplicationContext(  803): Unable to create files directory /data/data/com.perigee.brewing.recipeeditor.test/files
D/FileUtils(  803): Copying asset to internal file; assetName[TestFile_1.txt], internalFile[DestFile_1.txt]
W/ApplicationContext(  803): Unable to create files directory /data/data/com.perigee.brewing.recipeeditor.test/files
D/FileUtils(  803): Internal File Path: /DestFile_1.txt
W/System.err(  803): java.io.FileNotFoundException: /DestFile_1.txt (Read-only file system)
...
</code></pre>

<p>Is there a better way to do this type of testing?  Can data not be written to the internal file system in the test project?</p>

<p>Bottom line, if any one has any suggestions on how to test this I would appreciate it...  I have not been able to find much in the way of examples for testing this basic level of utility functions that require the use of the Context.  I have found a few references to testing whole utility libraries, but little details on managing the context in the tests.</p>
