Skip to content

Instantly share code, notes, and snippets.

@dupontgu
Created April 9, 2019 15:44
Show Gist options
  • Save dupontgu/6b7e0c6fab037f7d08a82c44533873b2 to your computer and use it in GitHub Desktop.
Save dupontgu/6b7e0c6fab037f7d08a82c44533873b2 to your computer and use it in GitHub Desktop.
Read raw file for Android unit tests
class BaseTest {
// assuming fileName is the full name of a file in /test/resources
protected fun readFile(fileName:String) : String {
javaClass.classLoader?.getResourceAsStream(fileName)?.let {
val result = ByteArrayOutputStream()
val buffer = ByteArray(1024)
var length = it.read(buffer)
while (length != -1) {
result.write(buffer, 0, length)
length = it.read(buffer)
}
return result.toString("UTF-8")
}
throw RuntimeException("Couldn't read file")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment