I want to write a Spek test in Kotlin. The test should read an HTML file from the src/test/resources
folder. How to do it?
class MySpec : Spek({
val fileContent = javaClass.getResource("/html/file.html").readText()
This is the way that I prefer to do it:
fun getResourceText(path: String): String {
return File(ClassLoader.getSystemResource(path).file).readText()
}
private fun loadResource(file: String) = {}::class.java.getResource(file).readText()
You might find the File class useful:
import java.io.File
fun main(args: Array<String>) {
val content = File("src/main/resources/input.txt").readText()
print(content)
}