diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8d918f..44b857f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,5 +32,18 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v4 - - name: Assemble (debug) - run: ./gradlew assembleDebug --no-daemon --stacktrace + - name: Unit tests (JUnit 5) + run: ./gradlew test --no-daemon --stacktrace + + - name: Assemble (debug) + compile instrumented tests + # assembleDebugAndroidTest compiles the Compose UI test; it runs on a device via + # connectedDebugAndroidTest (locally / on an emulator runner), not in this build job. + run: ./gradlew assembleDebug assembleDebugAndroidTest --no-daemon --stacktrace + + - name: Upload test reports + if: always() + uses: actions/upload-artifact@v4 + with: + name: test-reports + path: '**/build/reports/tests/' + if-no-files-found: ignore diff --git a/build-logic/convention/build.gradle.kts b/build-logic/convention/build.gradle.kts index 57110d6..d1109af 100644 --- a/build-logic/convention/build.gradle.kts +++ b/build-logic/convention/build.gradle.kts @@ -47,6 +47,10 @@ gradlePlugin { id = "architecture.domain.module" implementationClass = "com.example.architecture.convention.DomainModuleConventionPlugin" } + register("androidUnitTest") { + id = "architecture.android.unit.test" + implementationClass = "com.example.architecture.convention.AndroidUnitTestConventionPlugin" + } register("compose") { id = "architecture.compose" implementationClass = "com.example.architecture.convention.ComposeConventionPlugin" diff --git a/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidLibraryConventionPlugin.kt b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidLibraryConventionPlugin.kt index 0376d9d..2ce162a 100644 --- a/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidLibraryConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidLibraryConventionPlugin.kt @@ -19,6 +19,9 @@ class AndroidLibraryConventionPlugin : Plugin { defaultConfig { minSdk = MIN_SDK + // Used by instrumented (androidTest) tests, e.g. the Compose UI test in + // :feature:characters:presentation-compose. Harmless for modules without androidTest. + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } compileOptions { diff --git a/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidUnitTestConventionPlugin.kt b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidUnitTestConventionPlugin.kt new file mode 100644 index 0000000..47afb90 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/AndroidUnitTestConventionPlugin.kt @@ -0,0 +1,32 @@ +package com.example.architecture.convention + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.testing.Test +import org.gradle.kotlin.dsl.dependencies +import org.gradle.kotlin.dsl.withType + +/** + * Runs an Android library module's local unit tests (`src/test`) on the **JUnit 5 platform** with the + * shared `unit-test` toolset (JUnit Jupiter, kotlinx-coroutines-test, Turbine, AssertK). + * + * Deliberately does NOT use the `de.mannodermaus.android-junit5` Gradle plugin: its 1.11.x line + * targets AGP 8.x and we build on AGP 9.0. It isn't needed for *local* unit tests anyway — + * `com.android.build.gradle.tasks.factory.AndroidUnitTest` extends Gradle's [Test] task, so calling + * `useJUnitPlatform()` on it is enough (this mirrors `DomainModuleConventionPlugin`, which does the + * same for pure-JVM modules). + */ +class AndroidUnitTestConventionPlugin : Plugin { + override fun apply(target: Project) = with(target) { + dependencies { + add("testImplementation", libs.findBundle("unit-test").get()) + add("testRuntimeOnly", libs.findLibrary("junit-jupiter-engine").get()) + // Gradle 9 dropped the bundled launcher; JUnit 5 won't start without it. + add("testRuntimeOnly", libs.findLibrary("junit-platform-launcher").get()) + } + + tasks.withType().configureEach { + useJUnitPlatform() + } + } +} diff --git a/build-logic/convention/src/main/kotlin/com/example/architecture/convention/DomainModuleConventionPlugin.kt b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/DomainModuleConventionPlugin.kt index 82bc8bd..ebe663b 100644 --- a/build-logic/convention/src/main/kotlin/com/example/architecture/convention/DomainModuleConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/com/example/architecture/convention/DomainModuleConventionPlugin.kt @@ -21,6 +21,8 @@ class DomainModuleConventionPlugin : Plugin { add("testImplementation", libs.findLibrary("junit-jupiter-api").get()) add("testImplementation", libs.findLibrary("assertk").get()) add("testRuntimeOnly", libs.findLibrary("junit-jupiter-engine").get()) + // Gradle 9 dropped the bundled launcher; JUnit 5 won't start without it. + add("testRuntimeOnly", libs.findLibrary("junit-platform-launcher").get()) } tasks.withType().configureEach { diff --git a/feature/characters/data/build.gradle.kts b/feature/characters/data/build.gradle.kts index 6dd3fa9..509fa9c 100644 --- a/feature/characters/data/build.gradle.kts +++ b/feature/characters/data/build.gradle.kts @@ -2,6 +2,7 @@ plugins { alias(libs.plugins.architecture.android.library) alias(libs.plugins.architecture.koin) alias(libs.plugins.architecture.kotlinx.serialization) + alias(libs.plugins.architecture.android.unit.test) } android { @@ -12,4 +13,9 @@ dependencies { implementation(project(":core:domain")) implementation(project(":core:data")) implementation(project(":feature:characters:domain")) + + // Swap a Ktor MockEngine into HttpClientFactory.create(...) for the repository test. + testImplementation(libs.ktor.client.mock) + testImplementation(libs.ktor.client.content.negotiation) + testImplementation(libs.ktor.serialization.kotlinx.json) } diff --git a/feature/characters/presentation-compose/build.gradle.kts b/feature/characters/presentation-compose/build.gradle.kts index 016b9c0..98dd3b8 100644 --- a/feature/characters/presentation-compose/build.gradle.kts +++ b/feature/characters/presentation-compose/build.gradle.kts @@ -13,4 +13,9 @@ dependencies { implementation(project(":core:design-system")) implementation(project(":feature:characters:domain")) implementation(project(":feature:characters:presentation")) + + // Instrumented Compose UI test (robot pattern). The Compose convention already adds the BOM to + // androidTestImplementation; ui-test-manifest provides the empty Activity ComposeTestRule hosts in. + androidTestImplementation(libs.bundles.compose.ui.test) + debugImplementation(libs.androidx.compose.ui.test.manifest) } diff --git a/feature/characters/presentation/build.gradle.kts b/feature/characters/presentation/build.gradle.kts index e8e8a28..b443042 100644 --- a/feature/characters/presentation/build.gradle.kts +++ b/feature/characters/presentation/build.gradle.kts @@ -1,6 +1,7 @@ plugins { alias(libs.plugins.architecture.android.library) alias(libs.plugins.architecture.koin) + alias(libs.plugins.architecture.android.unit.test) } // UI-agnostic presentation: the MVI ViewModel + State/Action/Event live here and are shared by diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a9f9db4..d2143e4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -36,12 +36,10 @@ timber = "5.0.1" material = "1.12.0" # Testing -junit4 = "4.13.2" junitJupiter = "5.11.4" -androidJunit5 = "1.11.4" +junitPlatform = "1.11.4" turbine = "1.2.0" assertk = "0.28.1" -androidxTest = "1.7.0" androidxTestExt = "1.3.0" androidxTestRunner = "1.7.0" androidxEspresso = "3.7.0" @@ -97,7 +95,6 @@ koin-core = { module = "io.insert-koin:koin-core" } koin-android = { module = "io.insert-koin:koin-android" } koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose" } koin-test = { module = "io.insert-koin:koin-test" } -koin-test-junit5 = { module = "io.insert-koin:koin-test-junit5" } # --- Ktor --- ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } @@ -116,13 +113,12 @@ coil-network-okhttp = { module = "io.coil-kt.coil3:coil-network-okhttp", version timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" } # --- Testing --- -junit4 = { module = "junit:junit", version.ref = "junit4" } junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junitJupiter" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junitJupiter" } -junit-jupiter-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junitJupiter" } +# Gradle 9 no longer bundles the launcher — it must be on the test runtime classpath explicitly. +junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junitPlatform" } turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" } assertk = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertk" } -androidx-test-core = { module = "androidx.test:core", version.ref = "androidxTest" } androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTestRunner" } androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExt" } androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidxEspresso" } @@ -147,6 +143,16 @@ ktor = [ lifecycle-compose = ["androidx-lifecycle-runtime-compose", "androidx-lifecycle-viewmodel-compose"] views = ["androidx-appcompat", "material", "androidx-recyclerview", "androidx-fragment-ktx"] unit-test = ["junit-jupiter-api", "kotlinx-coroutines-test", "turbine", "assertk"] +# Instrumented Compose UI test (androidTest): ComposeTestRule + AndroidJUnit4 runner. +# espresso-core/runner are pinned to current versions: Compose's test rule drives Espresso's +# onIdle, and the transitive espresso 3.5.0 calls InputManager.getInstance() (removed on API 34+), +# which crashes on modern devices. 3.7.0 fixes that reflection. +compose-ui-test = [ + "androidx-compose-ui-test-junit4", + "androidx-test-ext-junit", + "androidx-test-espresso-core", + "androidx-test-runner", +] [plugins] # Upstream plugins @@ -155,8 +161,6 @@ android-library = { id = "com.android.library", version.ref = "agp" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } -# Declared for milestone 5 (ViewModel/Compose tests on Android); wired when tests land. -android-junit5 = { id = "de.mannodermaus.android-junit5", version.ref = "androidJunit5" } # Convention plugins (defined in :build-logic, resolved from the included build) architecture-android-application = { id = "architecture.android.application" } @@ -164,6 +168,7 @@ architecture-android-library = { id = "architecture.android.library" } architecture-android-feature = { id = "architecture.android.feature" } architecture-android-feature-views = { id = "architecture.android.feature.views" } architecture-domain-module = { id = "architecture.domain.module" } +architecture-android-unit-test = { id = "architecture.android.unit.test" } architecture-compose = { id = "architecture.compose" } architecture-koin = { id = "architecture.koin" } architecture-ktor = { id = "architecture.ktor" }