test infra: JUnit5 unit tests on Android modules + Compose UI test wiring

Add an architecture.android.unit.test convention plugin that runs local unit
tests on the JUnit5 platform via useJUnitPlatform() (AndroidUnitTest extends
Gradle's Test) + the unit-test bundle. Deliberately NOT using the
de.mannodermaus plugin (targets AGP 8.x; we're on AGP 9). Add
junit-platform-launcher (Gradle 9 dropped the bundled launcher); set the
instrumentation runner; add a compose-ui-test bundle pinning espresso/runner to
current versions (transitive espresso 3.5.0 calls the removed
InputManager.getInstance() on API 34+). CI now runs ./gradlew test and compiles
the instrumented tests. Drop unused testing catalog entries.
This commit is contained in:
2026-06-10 15:00:37 +02:00
parent cf63095acc
commit 7a7ab45a66
9 changed files with 82 additions and 11 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -19,6 +19,9 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
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 {

View File

@@ -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<Project> {
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<Test>().configureEach {
useJUnitPlatform()
}
}
}

View File

@@ -21,6 +21,8 @@ class DomainModuleConventionPlugin : Plugin<Project> {
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<Test>().configureEach {

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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" }