简体   繁体   中英

Robolectric Creating Dummy SharedPreferences of a Different Application

I have searched through posts here as well as Google for a solution to a problem that I am having with doing unit tests using Robolectric. One thing that I need to test involves looking up the shared preferences file that resides within another application. So, I don't need to generate/create/lookup the shared preferences file on the activity I am testing, I need the shared preferences file that resides in a different application. So, I need to trick the environment by thinking that this file exists for that application. So TLDR

  1. App1 performing unit tests on
  2. App1 accesses SharedPreferences of App2

Need to generate fake SharedPreferneces file of App2 so App1 can access and continue on with tests. Any help is appreciated. Thank you!.

I would first rework the code to achieve next:

  1. You have a class that works with any SharedPreferences file
  2. You have a class that opens/copy file for another application
  3. You have a class that orchestrate this by providing output of file reading class to shared preference processor class

And then the unit test would look like:

  1. Check if shared preferences class does required operations
  2. Check that class that copies/reads file from one location to another 3a. Check that orchestrator class pass correct paths to file operations class 3b. Check that orchestrator class pass result (stream, string, etc) to shared preferences class

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM