Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 979
dewijones92 Avatar asked Jul 26 '11 20:07

dewijones92


People also ask

What is the significance of Robolectric?

Robolectric provides a JVM compile version of the android. jar file. Robolectric handles views, resource loading, and many other things that are implemented in the Android native. This enables you to run your Android tests in your development environment, without requiring any other setup to run the test.

What is Robolectric testing?

Robolectric is a framework that brings fast and reliable unit tests to Android. Tests run inside the JVM on your workstation in seconds. With Robolectric you can write tests like this: @RunWith(RobolectricTestRunner.

What is Robolectric shadow?

Robolectric works by creating a runtime environment that includes the real Android framework code. This means when your tests or code under test calls into the Android framework you get a more realistic experience as for the most part the same code is executed as would be on a real device.


1 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
}
like image 155
Tom Avatar answered Oct 13 '22 01:10

Tom