Andreas Unterweger
09/12/2019, 1:06 PMfun String.resourceContent() : String = javaClass.getResource(this).readText()
if i call "/token.json".resourceContent()
in the test i get null
if i call javaClass.getResource("/token.json")
in the test it works
why does this not work in an extension function?christophsturm
09/12/2019, 1:18 PMMarat Akhin
09/12/2019, 1:32 PMjavaClass
points to the Class
object of java.lang.String
(which does not have any resources you are trying to load), and in your tests it most probably points to the Class
object of your test class.
UPD: not java.lang.String
, but kotlin.String
😃Andreas Unterweger
09/12/2019, 2:11 PMAndreas Unterweger
09/12/2019, 2:12 PMAndreas Unterweger
11/29/2019, 7:05 PMinline fun String.resourceContent() : String = object{}.javaClass.getResource(this).readText()
note the use of inline and object{} to create a class whos loader is same as where the method gets called.
the version before might not find the resource if called from a different module