I'm following this post, http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/, to share code, but how to share an asset?, like a fixture file?, I want to mock an api response, so I have a JSON file to do it, but I try this: https://gist.github.com/nebiros/91a68aaf6995fa635507
In Unit Test, this works:
ClassLoader.getSystemResourceAsStream("some_response.json");
but in Android Intrumentation Tests, it doesn't, where can I put those fixture files?.
Instrumented tests run on Android devices, whether physical or emulated. As such, they can take advantage of the Android framework APIs. Instrumented tests therefore provide more fidelity than local tests, though they run much more slowly.
To add a testing source set for your build variant in Android Studio, follow these steps: In the Project window on the left, click the drop-down menu and select the Project view. Within the appropriate module folder, right-click the src folder and click New > Directory.
I found this way, to share fixture files from test
to androidTest
:
resources
folder of your test
one, here: src/test/resources
.Add test
resources folder to androidTest
, resources.srcDirs += ['src/test/resources']
, here's an example:
android {
sourceSets {
String sharedTestJavaDir = 'src/sharedTest/java'
test {
java.srcDirs += [sharedTestJavaDir]
}
androidTest {
java.srcDirs += [sharedTestJavaDir]
resources.srcDirs += ['src/test/resources']
}
}
}
Access fixture files from your androidTest
env this way: this.getClass().getClassLoader().getResourceAsStream(filename);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With