T
T
Tsuzukeru2021-05-21 19:54:42
Android
Tsuzukeru, 2021-05-21 19:54:42

How to get the full path to a file from the resource directory in the test folder?

You need to test the correctness of the Json conversion. To do this, in the test directory, I created the resources directory , where I put the .json extension file.

In the test, I refer to it like this:

class RawMediaPreviewResponseConverterTest {
    val jsonPath = "src/test/resources/RawMediaPreviewResponse.json"
    val gson = Gson()

    @Test
    fun `media previews response converter should convert images`(){
        val rawMediaPreviewResponse = gson.fromJson(FileReader(jsonPath),RawMediaPreviewResponse::class.java)
        ...
    }
}


This is hardcode. If you change the structure of the file location, then everything will break.
Is it possible to get the path to the .json file somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-05-22
@zagayevskiy

val classloader = Thread.currentThread().getContextClassLoader()
val is = classloader.getResourceAsStream("RawMediaPreviewResponse.json")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question