# Accessing Test Resources using Kotlin Multiplatform

DevFeed: [Accessing Test Resources using Kotlin Multiplatform](<https://devfeed.tech/articles/accessing-test-resources-using-kotlin-multiplatform-25113.md>)

Original publisher: [Read original article](<http://michaelevans.org/blog/2024/04/17/accessing-test-resources-using-kotlin-multiplatform/>)

Author: Michael Evans

Published: 2024-04-18T00:11:01Z

Content type: tutorial

Language: en

Sources: [Gadget Habit](<https://devfeed.tech/sources/gadget-habit.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [multiplatform](<https://devfeed.tech/topics/multiplatform.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Android](<https://devfeed.tech/topics/android.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [JSON](<https://devfeed.tech/topics/json.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [ios](<https://devfeed.tech/tags/ios.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [library](<https://devfeed.tech/tags/library.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This tutorial explains how to access test resources such as JSON files and configuration data in Kotlin Multiplatform projects. It describes organizing resources per target platform and using platform-specific loaders with shared test logic.

## Source excerpt

Kotlin Multiplatform (KMP) offers a powerful way to share code across platforms like Android, iOS, and the JVM. However, when it comes to testing, you might encounter a common challenge: how to handle test resources such as JSON files, configurations, or other data needed for tests. In this post, we'll dive into strategies for accessing test resources in KMP projects. Why Test Resources Matter Test resources are essential for validating your code against real-world scenarios. For example, if you're writing a library to parse JSON, you'd want to test it against diverse JSON samples representing different edge cases. While resource access is straightforward in single-platform projects, KMP's multi-target nature requires some additional setup. The Challenge in Kotlin Multiplatform In KMP, test code resides in the commonTest source set, but resources aren't directly bundled with it. Each platform manages file paths and resource access differently, so you need platform-specific setups for your resources and shared logic to load them efficiently. Organizing Test Resources Start by structuring your resources in a way that aligns with your project layout: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 project-root/ src/ commonTest/ kotlin/ ... androidTest/ resources/ test-data.json jvmTest/ resources/ test-data.json iosTest/ resources/ test-data.json This setup ensures that each target platform can access its respective resources while keeping them logically grouped. Loading Resources by Platform For Android, place your test resources in the androidTest/resources directory. Use the javaClass.classLoader to load them: 1 2 3 4 5 fun loadTestResource(resourceName: String): String { val inputStream = javaClass.classLoader?.getResourceAsStream(resourceName) return inputStream?.bufferedReader()?.use { it.readText() } ?: throw IllegalArgumentException("Resource not found: $resourceName") } For iOS, add your resources to the test target in Xcode. Use platform-specific code to load them: 1 2 3 4