How can I access resources in src/main/resources f...
# spring
h
How can I access resources in src/main/resources from test?
google 1
j
Same way you access them normally
main
is included in
test
h
Copy code
val t = ClassPathResource("$fileName.csv")
val u = t.inputStream

val reader = BufferedReader(u.reader())
var content: String = ""
reader.use { reader ->
    content = reader.readText()
}

println(content)

<http://java.io|java.io>.FileNotFoundException: class path resource [material_surcharges.csv] cannot be opened because it does not exist
In my unit test. I had to use:
Copy code
val path = Paths.get("src", "main", "resources", "csv", "$fileName.csv")
val stream = Files.newInputStream(path)
j
@hooliooo I have to admit, I did not try that class yet. What about the "normal"
getResource("/$fileName.csv")
version? It needs the additional slash to make it an absolut path, but should work. Resources from main should be available in test. Otherwise neither your default application.yaml config file nor logging would be available.
h
Copy code
val url = javaClass.classLoader.getResource("/$fileName.csv")
yields null
j
That is a little strange. I have to admit, it's been a while since I used this. At this point, I think it's more a matter of the build tool, of how it creates the runtime environment.
Huh, google does not say either way at first glance. Try adding src/main/resources to the resource directories in the build file, see if it changes anything. Maybe my memory was wrong. Something like this:
Copy code
sourceSets {
    test {
        resources {
            srcDirs("src/main/resources")
        }
    }
}
n
how is related to kotlin? ­čĄö
­čĺ» 1
159 Views