dewijones92
dewijones92

Reputation: 1359

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

Upvotes: 9

Views: 6998

Answers (3)

Tom
Tom

Reputation: 7794

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
}

Upvotes: 8

Virag Brahme
Virag Brahme

Reputation: 2062

I have done this as :

int resourceId = Robolectric.getShadowApplication().getResources()
                 .getIdentifier("myaudio","raw", Robolectric.getShadowApplication().getPackageName()); 

if (resourceId != 0) {  // Raw folder contains resource.
    assertTrue(true);
} else {                // Raw folder doesn't contain resource.
    assertTrue(false);
}

Hope this help...!! Thanks..!!

Upvotes: -1

dewijones92
dewijones92

Reputation: 1359

wooops, I really should do more research before I post questions
robolectric has got resource support https://github.com/robolectric/robolectric

Upvotes: 0

Related Questions