Created
April 9, 2019 15:44
-
-
Save dupontgu/6b7e0c6fab037f7d08a82c44533873b2 to your computer and use it in GitHub Desktop.
Read raw file for Android unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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