Trying to make fun of Environment.getExternalStorageState

For several hours, I tried to figure out how to mock the call to Environment.getExternalStorateState() while unit testing my Android application.

I managed to scoff at SystemServices, Providers and Services, but I can't figure out how to scoff at this call, as it is not a call to something provided in my context, but something to the OS environment.

I would be grateful for the help.

+6
source share
2 answers

You can write an assistant around this call and easily make fun of it after (sorry for the presence of an auxiliary part in the class name):

 public class EnvironmentHelper { public String getStorageState() { return Environment.getExternalStorateState(); } } 

Or, if you use Robolectric , you can call:

 ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED); 

It depends on your settings and needs, but I would recommend investing in Robolectric using

+9
source

I just worked on completing the call to the Environment method in my test helper class so that I could deride the status of the SD card as I wanted, depending on the variable that I could set as desired in each test case. The best solution, I think.

0
source

Source: https://habr.com/ru/post/959255/


All Articles