Status Update
Comments
eu...@gmail.com <eu...@gmail.com> #2
Ah sorry I forgot versions
Kotlin - 1.6.10
Gradle - 7.4
AGP - 7.2.0-beta02
eu...@gmail.com <eu...@gmail.com> #3
And super thank you for making it happening!
xa...@google.com <xa...@google.com>
sa...@gmail.com <sa...@gmail.com> #4
am...@google.com <am...@google.com> #5
pa...@gmail.com <pa...@gmail.com> #6
YouTrack issue is closed as "Third Party", ball is back at Google's side.
Duplicate this against
xa...@gmail.com <xa...@gmail.com> #7
Note that the problem only exists for Kotlin. If I have a sourceSet `testFixture` with Java classes, they I can use them from a different module, but it doesn't work at all with Kotlin.
to...@gmail.com <to...@gmail.com> #8
za...@scotchnetworks.com <za...@scotchnetworks.com> #9
I was able to find a solution, not sure if every step is required but this is what worked for me.
Tested working with: AGP: 8.7.2, Gradle: 8.9, Kotlin: 2.0.0 / 2.0.20
Does not work with: AGP: 8.4.2, Gradle: 8.7 : Error - IDE Linter can find the class, no syntax highlight errors, but get Unresolved Reference upon execution.
Procedure
- It's not necessary to convert classes Java.
- Move your
testFixtures
directory to your:app
module - Modify the package name to match whatever you have in
android.namespace
(not sure if this is required) - Add a sourceSet to :app/build.gradle.kts
sourceSets { ... getByName("testFixtures") { java.srcDir("src/testFixtures/java") //my files are in the java folder resources.srcDir("src/testFixtures/resources") }
- Remove the plugin
id(java-test-fixtures)
(everywhere) - In the
build.gradle.kts
of the implementing module use:
testImplementation(testFixtures(projects.app))
ortestImplementation(testFixtures(project(":app"))
// Counter to intuition you won't create a circular reference. - You need to declare
testFixturesImplementation
dependencies in:app/build.gradle.kts
.
- My project never asked for it before but required kotlin runtime and compose runtime on the classpath.
- It also required me to add
composeOptions
which I never had before
Now my :app/build.gradle.kts additions look like this
android {
...
composeOptions {
kotlinCompilerExtensionVersion = "1.5.15"
}
...
sourceSets {
...
getByName("testFixtures") {
java.srcDir("src/testFixtures/java")
resources.srcDir("src/testFixtures/resources")
}
...
}
}
...
dependencies {
...
implementation(libs.androidx.compose.runtime) //new
testFixturesImplementation(libs.junit)
testFixturesImplementation(libs.androidx.test.core)
testFixturesImplementation(libs.androidx.test.runner)
testFixturesImplementation(libs.androidx.junit.core.ktx)
testFixturesImplementation(libs.androidx.arch.core.testing)
testFixturesImplementation(libs.timber)
testFixturesCompileOnly(libs.kotlin.stdlib) // I believe this is required
testFixturesRuntimeOnly(libs.kotlin.stdlib) // I believe this is required
testFixturesImplementation(libs.androidx.compose.runtime)
...
}
I also have this in gradle.properties
android.experimental.enableTestFixtures=true
android.experimental.enableTestFixturesKotlinSupport=true
More info:
For example:
Say we want to access the proper objects in :domain/SomeTest.kt
in order to successfully execute this line:
val fakeItem = json.decodeFromString<ItemEntity>(loadJsonFromResources("fakeitementity.json")).toDomain()
where .toDomain()
and ItemEntity
is from data
module,
and fakeitementity.json
is located in the :app
module: project/app/src/testFixtures/resources
In order to share the app
, domain
, and data
test fixtures will need this in :app/build.gradle.kts
testImplementation(projects.domain)
testImplementation(projects.data)
testFixturesImplementation(projects.domain)
testFixturesImplementation(projects.data)
To use them in the domain tests, you will need to add the module and its testFixtures in the :domain/build.gradle.kts
testImplementation(projects.domain)
testImplementation(projects.data)
testImplementation(testFixtures(projects.app))
testImplementation(testFixtures(projects.domain))
clean/build the app and wait for success!
Description
Android Studio Chipmunk supports test fixtures in Kotlin and Java - class importing and autocomplete is working great. However, the gradle unit test task fails with unresolved reference error.
Steps to reproduce:
UserMapperKtTest
test class:core:testDebugUnitTest
in console