kotlin - 如何从 Kotlin 的资源中读取文本文件?

我想用 Kotlin 编写一个 Spek 测试。测试应该从 src/test/resources 文件夹中读取一个 HTML 文件。怎么做?

class MySpec : Spek({

    describe("blah blah") {

        given("blah blah") {

            var fileContent : String = ""

            beforeEachTest {
                // How to read the file file.html in src/test/resources/html
                fileContent = ...  
            }

            it("should blah blah") {
                ...
            }
        }
    }
})

最佳答案

val fileContent = MySpec::class.java.getResource("/html/file.html").readText()

https://stackoverflow.com/questions/42739807/

相关文章:

java - 是否可以在 Kotlin 中使用 Mockito?

kotlin - 有什么作用? : do in Kotlin?(猫王运算符)

kotlin - Kotlin 协程如何比 RxKotlin 更好?

kotlin - 在 Kotlin 中进行空检查的最佳方法?

algorithm - Kotlin - 从数组中删除重复字符串的惯用方法?

kotlin - 函数中Unit-return的目的是什么

database - Android Room Persistence 库和 Kotlin

list - 如何在 Kotlin 中初始化 List

spring - 如何在kotlin中使用@Autowired之类的spring注解?

java - 为什么 Kotlin 中没有并发关键字?