Kotlin のリソースからテキストファイルを読み取るにはどうすればいいですか? 質問する

Kotlin のリソースからテキストファイルを読み取るにはどうすればいいですか? 質問する

Kotlin で Spek テストを記述したいです。フォルダー
から HTML ファイルを読み取るにはどうすればよいでしょうかsrc/test/resources?

class MySpec : Spek(
    {
        describe("blah blah") {
            given("blah blah") {
                var fileContent: String = ""
                beforeEachTest {
                    // How to read the file.html in src/test/resources/html/
                    fileContent = ...
                }
                it("should blah blah") {
                    ...
                }
            }
        }
    }
)

ベストアンサー1

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

おすすめ記事