How to access files from assets folder during tests execution?

后端 未结 8 2084
旧时难觅i
旧时难觅i 2021-02-01 20:25

How to access files from assets folder during unit tests execution? My project is build using Gradle, I use Robolectric to run tests. It seems like gradle is being recognizing t

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 21:07

    I've just got stuck with the same issue and here is how it works for me.

    I put test files to src/test/resources folder instead of assets folder.

    Then I get these files as stream in following way

    private InputStream openFile(String filename) throws IOException {
           return getClass().getClassLoader().getResourceAsStream(filename);
    }
    

    filename is the relative path to the file inside resources folder.

    That's it. I've found solution at Robolectric github

提交回复
热议问题