How to share resources between unit test and instrumentation test in android?

非 Y 不嫁゛ 提交于 2019-12-10 12:39:34

问题


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


回答1:


I found this way, to share fixture files from test to androidTest:

  • Add your fixture files to the 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);



来源:https://stackoverflow.com/questions/34256623/how-to-share-resources-between-unit-test-and-instrumentation-test-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!