createComposeRule() 未导入并且依赖项不起作用

问题描述 投票:0回答:1

我正在尝试将 composeTestRule 添加到我的测试中,但是,我需要导入 createComposeRule() 才能工作。问题是它要求我添加一个依赖项,而添加后它不会执行任何操作。我尝试仅用

testImplementation
交替依赖关系,但后来我无法将其变为
setContent
。我也尝试过使缓存失效、清理和重建,但到目前为止没有任何效果。

测试文件:

package com.junclabs.city

import org.junit.Test
import org.junit.Assert.*
import org.junit.Rule

class ExampleUnitTest {
    @get:Rule
    val composeTestRule = createComposeRule()
    @Test
    fun navigateToParkSuccessfully() {
        composeTestRule.setContent {
            MainActivity()
        }
    }
}

构建.gradle:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.junclabs.city"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.junclabs.city"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.6.5@aar")
    implementation("androidx.navigation:navigation-compose:2.7.7")
    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.08.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.08.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}
kotlin android-gradle-plugin android-jetpack-compose build.gradle
1个回答
1
投票

创建一个空项目并添加依赖项和

ExampleUnitTest
后...它可以工作

尝试将项目保存在 github 上的私人存储库中,在本地删除它并拉取它(因为你的本地缓存肯定有问题)

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.