title: Write/create file within Android Instrumentation context
body: <p>I have a class that creates (or opens) a file to write some data to it. This class receives a Context in the constructor, saves it in an instance field, and then uses it to call the context.openFileOutput method.</p>
  
  <p>When running the application, I instantiate this class by passing the ApplicationContext as the Context, and everything is working as expected.</p>
  
  <p>However, when I try to test this class with an Instrumentation Test, I get a <code>NullPointerException</code>. I'm passing the <code>getInstrumentation().getContext()</code> context, which I know corresponds to the context of the test, and not the one of the real app.</p>
  
  <pre><code>getInstrumentation().getContext().openFileOutput("myFile", Context.MODE_PRIVATE); // This throws NullPointerException :( :(
  </code></pre>
  
  <p>Within the test, I need this file to be created in the test package and not in the app package, as I don't want overwrite the file in my app.</p>
  
  <p>I know there is a <code>RenamingDelegatingContext</code> class out there, but I cannot pass this context to my class since my class also opens a raw resource, and I want that resource to be different when running the test (something like a mocked resource).</p>
  
  <p>I searched a lot about this, and there is no documentation about the Instrumentation Context. I couldn't find its limitations nor anything that solves my problem.</p>
  
  <p>Do you know how to tackle this?</p>