如何编写需要 Android 上下文的集成测试?

How can one write an Integration Test that requires an Android Context?

例如,测试input/output到一个图像文件。要创建文件,使用以下代码(摘要):

 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;

当我尝试使用此代码编写集成测试以创建文件时,getExternalFilesDirthis(这是一个上下文)出现问题。如何在不通过 activity 或上下文的情况下进行有效测试?我可以生成一个吗?

在仪器测试中,InstrumentationRegistry.getTargetContext() returns 一个 Context 用于被测试的包。