Прочитать файл json из androidTest

привет, я пытаюсь прочитать файл json, расположенный в папке ресурсов в androidTest. Я могу получить доступ к файлу json, расположенному в папке ресурсов в Test/resources, но не в androidTest/resources.

Я использую следующее, чтобы получить файл json в папке Test/resources.

private fun getJson(path: String): String {
    val uri = this.javaClass.classLoader?.getResource(path)
    val file = File(uri?.path ?: "")
    return String(file.readBytes())
}

Есть ли способ получить доступ к файлу в androidTest/resources? Прочитать файл json из androidTest

1
0
488
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий
 private fun getJson(fileName: Int): String {
        val inputStream = InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(fileName)
        val s = Scanner(inputStream).useDelimiter("\\A")
        return if (s.hasNext()) s.next() else ""
    }

Другие вопросы по теме