How do I get access to resources in my test classes when using robolectric

Viewed 5610

I have made a text file of values that I want to use for testing in res/raw
I want to use them in testing
I am using robolectric
What is the best way of accessing these values?
Thanks

3 Answers

You can access your app's resources via your Application instance. Use ApplicationProvider to fetch it from within a unit test:

// replace 'Application' with your actual class if you use a custom one
ApplicationProvider.getApplicationContext<Application>().resources

Ensure includeAndroidResources is set to true in your build.gradle or your unit tests won't see your resources:

android {
    testOptions.unitTests.includeAndroidResources true
}
Related