Reputation: 529
I am developing an android library.
The library takes in some parameters. For instance, it has a setStyle method which takes in a reference to a style resource:
fun setStyle(@StyleRes style: Int) ...
I want to test this method. To do this, I want to pass a resource TestStyle
to the library in a unit test. Something like this:
myLibrary.setStyle(R.style.TestStyle)
However, I don't want to create the TestStyle
resource in the main src/main/res/values/styles.xml
because this would make the resource available to people using my library. I want it to only be available in tests.
Instead, I tried to place the TestStyle
<item />
in a new test-only file at src/test/res/values/styles.xml
. However, I can't figure out how to access this in the unit test, as it is not available through R.
How can I create resources that are only accessible in tests and not in other parts of the code?
Upvotes: 5
Views: 1895
Reputation: 1690
Place your resources in some folder. For example "src/test/res". And use the following gradle configuration:
android {
...
testOptions {
unitTests.includeAndroidResources = true
}
}
tasks.withType(Test) {
android.sourceSets.main.res.srcDirs += 'src/test/res'
}
This way, the resources will be visible for debug and release test builds, but they will not be packaged to your library or app.
Upvotes: 7