unit test with mocked Json

Asked

Viewed 239 times

1

I am trying to create a test from a json file that I have with the mocks of the Api response. But when I run the unit test the variable stream the return is null, ie it can’t read the file resulting always in failure. Is there anything I can do to fix this reading problem that is returning null? Or if there is some other way to read Json files and convert them into model classes for testing purposes?

@Test
fun testGetHeroesWhenError404(){
    val resulr = readFile("mock_response_successfully")
    assertTrue(true)
}

private fun readFile(file: String): ReturnData {
    val stream = this.javaClass.classLoader!!.getResourceAsStream(file)
    val bf = BufferedReader(InputStreamReader(stream) as Reader?)
    return Gson().fromJson(bf, ReturnData::class.java)
    //return BufferedReader(InputStreamReader(stream))
}

The initial problem is on this line:

val stream = this.javaClass.classLoader!!.getResourceAsStream(file)

For the stream receives null causing a NullPointerException

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.