https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Alejandro Moya

08/17/2022, 6:49 PM
Good afternoon, quick question, I’m implementing some tests on my kmm project and wanted to save the json payloads on separate files, is there a way to load those? from the jvm test env?
j

Jeff Lockhart

08/17/2022, 7:55 PM
These aren't common tests? Just JVM tests?
a

Alejandro Moya

08/17/2022, 8:03 PM
They are the common but run on jvm
j

Jeff Lockhart

08/17/2022, 8:13 PM
What other targets are you supporting? Are you only running the common tests on the JVM and not the other targets?
I'd recommend putting the tests in jvmTest if you only intend to run them with the jvm target. If they're testing common code, it's good to have them run on all supported platform targets.
There is no common API for loading resources, but you can create expect/actual implementations with a common interface. Besides the common runtime API, you also need to ensure the resources are included in the runtime .jar or in the binary's current directory or other app bundle. How to do this varies slightly depending on the platform target.
I've found some targets handle embedding resources in their own resources directory by default, others need some gradle Copy task configuration to work. JVM likely works (I know Android does). Native doesn't, however. None of them seem to support copying resources from the common source set though, without explicitly configuring this directory as a resources source.
a

Alejandro Moya

08/17/2022, 9:42 PM
This is just for testing purposes and i have the library built independently from the app that consumes it
Since all the logic resides in the common with a small list of adaptations for iOS, there's no point in implementing an entire project to run the tests
j

Jeff Lockhart

08/17/2022, 10:32 PM
If you include the json files in the jvmTest/resources folder, they should be available to load from the .jar at runtime with something like
javaClass.getResource("path/to/resource.json").readText()
.
a

Alejandro Moya

08/17/2022, 11:43 PM
Thanks!
3 Views