3

I'd like to use a different resource directory for one of my Robolectric test cases. In this case i'd like to use the release-variant resources. The thing is I can't get this to work and I'm not seeing examples online of anyone using this config. It still uses the debug resources (Im running gradle testDebug where 'Debug' is one of my buildTypes in Gradle). The directory i'm setting is correct relative to the directory where the manifest is. I've tried referencing the ref files in "build/intermediates/.." too. The configuration seems to have no effect at all.

@RunWith(RobolectricTestRunner.class)
@Config(resourceDir = "../../variant_resources/release/res")

..

@Test
public void testHowThingsAreUsingReleaseResources() {
  ..
}

Im using Robolectric 2.4-SNAPSHOT Has anyone had had better luck using this Configuration? Is there an obvious mistake?

erafn
  • 191
  • 7
  • I can also add that the resourceDir parameter does not seem to work in org.robolectric.Config.properties either. The properties file in itself works (manifest for example will change the manifest used) but the resourceDir parameter seems like it's not recognized by Robolectric at all – erafn Oct 30 '14 at 20:07

1 Answers1

0

Qualified Resources

If the resource file is to vary based on the sub-folder, then it's possible to specify a qualifier for the resource. See the RoboElectric page on Qualified Resources.

The @Config attribute with the qualifiers parameter can be used to specify the qualifier for the resource.

For example, if there are resources:

 values/strings.xml
 values-en/strings.xml
 values-en-port/strings.xml

... then the attribute @Config(qualifiers="en-port") would ensure that the values-en-port/strings.xml resource would be used.

Changing resource paths using a different RobolectricTestRunner

Another approach to load different resources (if this is only to apply to some tests) is to implement a different implementation of the RobolectricTestRunner class, that loads different resource paths. There's a good example of how to do this here:

https://stackoverflow.com/a/29223625/3063884

Community
  • 1
  • 1
CJBS
  • 15,147
  • 6
  • 86
  • 135