未解决的引用:junit4:Android ComposeUI 测试

9

我在尝试使用Jetpack Compose构建的UI进行测试,并添加了以下依赖项来设置它

    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.1.0"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.0.5"

为了测试,我刚开始编写以下内容

class LoginActivityComposeTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun socialPluginsTest() {
        composeTestRule.setContent {
            SocialLogins()
        }
    }
}

我尝试导入的 createComposeRule() 方法无法使用。我已经尝试手动导入 import androidx.compose.ui.test.junit4.createComposeRule,但是在此之后运行测试时会出现错误 Unresolved reference: junit4。这是我正在使用的完整 build.gradle 依赖项:
compose_version = '1.0.1' retrofit_version = '2.9.0'
    implementation "androidx.core:core-ktx:1.7.0"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
    implementation "androidx.activity:activity-compose:1.4.0"
    implementation "androidx.navigation:navigation-compose:2.5.0-alpha01"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0"
    implementation 'com.google.android.gms:play-services-auth:20.1.0'
    implementation "io.coil-kt:coil-compose:1.4.0"
    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.1.0"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.0.5"


你找到解决方法了吗?我也遇到了同样的问题。 - swalkner
不,我没有。我暂停了这个副业项目的工作,因为我很忙。 - P-RAD
你确定这是正确的导入吗?我建议您查阅文档进行双重检查。(此处附上文档链接) - Code-Apprentice
4
将测试文件移动到(androidTest)文件夹后,问题得以解决。 - Sam
6个回答

6

我把测试文件放错了文件夹,不是放在测试文件夹里,而是放在 androidTest 文件夹里。


1

你可能需要在测试类的顶部添加@RunWith(AndroidJUnit4::class)

我不能百分之百确定,因为我不确定你的gradle设置中是否还有其他原因导致这个问题,但我曾遇到过类似的问题,那就是我的解决方案。


0
根据@nguyen-hoà的说法,使用testImplementation而不是androidTestImplementation。这取决于测试文件在项目结构中的位置。 我用这个配置解决了这个问题。如果你使用模块化,请确保导入在你的模块的正确gradle文件中。

0
如果你正在使用测试文件夹,可以使用以下方式: testImplementation,对于androidTest文件夹,使用androidTestImplementation

0

我需要重新启动Android Studio才能正确地找到并导入所需的androidx.compose.ui.test.*


0
导入依赖项如下所示:
androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.5.1")

1
您的回答可以通过提供更多支持性信息来改进。请[编辑]以添加更多细节,如引用或文献,以便他人可以确认您的回答是否正确。您可以在帮助中心找到更多关于如何撰写好答案的信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接