第2章 项目依赖配置:添加Espresso、Compose UI测试、JUnit4依赖

2.1 依赖配置的层次与角色

在混合夹具项目中,Espresso与Compose UI测试共享同一个Android测试运行环境,但依赖项需要遵循明确的层级规则。Gradle依赖配置的核心目标包括:

  • 提供AndroidX Test基础库以运行Instrumented测试。
  • 引入JUnit4测试框架作为测试用例的执行引擎。
  • 添加Espresso核心库用于传统View交互。
  • 添加Compose UI测试库用于Composable组件的断言与操作。
  • 确保所有库版本兼容,避免运行时ClassNotFoundException或NoSuchMethodError。

2.2 依赖声明与版本规范

以下表格列出了混合夹具项目所需的全部测试依赖项及其用途:

依赖项 Group / Artifact 作用域
AndroidX Test Runner androidx.test:runner 测试运行器(必须)
AndroidX Test Rules androidx.test:rules 测试规则辅助
JUnit4 junit:junit 测试框架(必须)
Espresso Core androidx.test.espresso:espresso-core 传统View交互
Espresso Intents androidx.test.espresso:espresso-intents Intent测试(可选)
Compose UI Test androidx.compose.ui:ui-test-junit4 Compose UI测试
Compose UI Test Manifest androidx.compose.ui:ui-test-manifest Compose测试初始化
版本兼容提示:Compose UI Test 的版本必须与项目中使用的 Compose BOM 版本完全一致。Espresso 推荐使用 3.5.1+,AndroidX Test 推荐使用 1.5.0+,JUnit4 使用 4.13.2。混合使用时,androidx.test:runner 和 androidx.test.espresso:espresso-core 必须同时升级,避免内部依赖冲突。

2.3 完整Gradle依赖配置(app/build.gradle)

在模块级别的 build.gradle 文件中,将以下代码添加到 dependencies 块内。注意使用 androidTestImplementation 而非 testImplementation,因为Espresso和Compose UI测试运行在设备/模拟器上。

android {
    // 确保已设置测试运行器
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    // 启用Compose编译选项
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.4"
    }
}

dependencies {
    // 核心测试框架
    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test:rules:1.5.0"
    androidTestImplementation "junit:junit:4.13.2"

    // Espresso(用于传统View测试)
    androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
    androidTestImplementation "androidx.test.espresso:espresso-intents:3.5.1"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:3.5.1"

    // Compose UI 测试(用于Composable组件)
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.5.4"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.5.4"
}

// 统一管理Compose版本(可选但推荐)
compose {
    kotlinCompilerExtensionVersion = "1.5.4"
}
常见错误:重复依赖版本不匹配
若项目中同时引入了androidx.compose.ui:ui-test-junit4和androidx.test.espresso:espresso-core,务必保证appcompat、activity-compose等库的版本均来自同一BOM(如Compose BOM 2023.10.01),否则可能抛出LinkageError或NoOpener异常。

2.4 配置依赖关系流程图

以下SVG展示了JUnit4、Espresso与Compose UI Test在测试运行器下的依赖层级与数据流关系:

混合夹具依赖配置体系 JUnit4 测试框架 Espresso Core Compose UI Test AndroidX Test Runner Android 设备 / 模拟器 浅色节点:依赖端;深色节点:容器

2.5 验证配置是否生效

配置完成后,执行以下命令验证依赖是否成功解析:

./gradlew :app:dependencies --configuration androidTestDebugCompileclasspath

# 检查输出中是否包含以下关键行:
# +--- junit:junit:4.13.2
# +--- androidx.test.espresso:espresso-core:3.5.1
# +--- androidx.compose.ui:ui-test-junit4:1.5.4
# \--- androidx.test:runner:1.5.2

若出现冲突警告,使用 ./gradlew :app:dependencyInsight --dependency <artifactId> --configuration androidTestDebugCompileClasspath 定位原因。

调试要点:如果运行测试时提示 No tests found,请检查 testInstrumentationRunner 是否配置为 androidx.test.runner.AndroidJUnitRunner,并确认 debugImplementation "androidx.compose.ui:ui-test-manifest:1.5.4" 已添加(只在debug变体生效,不影响release包)。