P-RAD
P-RAD

Reputation: 1341

Unresolved reference: junit4: Android ComposeUI testing

I was trying to test the UI I built using jetpack compose in android and added these dependancies to set it up

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

for the testing I was just starting to write the following

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

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

The createComposeRule() which I am trying to import was not available. I've tried to manually import from import androidx.compose.ui.test.junit4.createComposeRule But when I try to run the tests after that there is an error showing

Unresolved reference: junit4

Here is the full build.gradle dependancies I am using

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"

Upvotes: 9

Views: 5290

Answers (7)

Istiqomah Sumadikarta
Istiqomah Sumadikarta

Reputation: 91

I placed the test file in the wrong folder. I put it in the test folder but it should be in the androidTest folder.

Upvotes: 9

Konrad
Konrad

Reputation: 72

Maybe that will be useful for somebody. Even thou I have BOM: implementation(platform("androidx.compose:compose-bom:2024.01.00")) I had to still add version for androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.6.0")

When I had only the dependencie set for BOM androidTestImplementation("androidx.compose.ui:ui-test-junit4") the Gradle could not find it.

Upvotes: 0

thecoder93
thecoder93

Reputation: 31

As said from @nguyen-hoà use testImplementation instead of androidTestImplementation. This depends from position test files in the structure of your project. I solved with this configuration. If you use modularization, verify that import is in correct gradle file of your module.

Upvotes: 1

Nguyen Hoà
Nguyen Hoà

Reputation: 353

If you are using test folder using this: testImplementation, with androidTest folder using androidTestImplementation

Upvotes: 1

Sina Pirooz
Sina Pirooz

Reputation: 1

import dependency like following:

androidTestImplementation("androidx.compose.ui:ui-test-junit4-android:1.5.1")

Upvotes: 0

mto1
mto1

Reputation: 123

I needed to restart Android Studio for it to correctly find and import the required androidx.compose.ui.test.*

Upvotes: 0

Jüri Kiin
Jüri Kiin

Reputation: 809

You might need to add @RunWith(AndroidJUnit4::class) to the top of your test class.

I can't say with 100% accuracy as I'm not sure what else might be causing that in your gradle setup, but I've ran into a similar issue, and that was my solution.

Upvotes: 1

Related Questions