2

For example, testing input/output to an image file. To create the file, the following code is used (summary):

 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
 File image = File.createTempFile("tempImage",".jpg",storageDir);
 mCurrentPhotoPath = image.getAbsolutePath();
 mCurrentPhotoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",image);
 File photoFile = image;

When I try to write an integration test using this code to create a file, I have issues with the getExternalFilesDir and this (which is a context). How can I effectively test without having an activity or context to pass? Can I generate one?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Evan Weissburg
  • 1,564
  • 2
  • 17
  • 38

1 Answers1

4

In an instrumentation test, InstrumentationRegistry.getTargetContext() returns a Context for the package being tested.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491