开发者

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

开发者 https://www.devze.com 2023-03-23 06:55 出处:网络
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 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

Wha开发者_Python百科t is the best way of accessing these values?

Thanks


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
}


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


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..!!

0

精彩评论

暂无评论...
验证码 换一张
取 消