简体   繁体   中英

Unable to run unit test in Android Studio

I´m trying to run unitary test in Android Studio but for some reason in my project i´m not able to do it.

Here is my gradle ->

    implementation project(':base')

    implementation "androidx.compose.ui:ui:$compose"
    implementation "androidx.activity:activity-compose:1.4.0"
    implementation "androidx.compose.material:material:$compose"
    implementation "androidx.compose.ui:ui-tooling:$compose"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    implementation "com.google.code.gson:gson:$gson_version"
    implementation "androidx.room:room-ktx:$room_version"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swipe_refresh"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutines_version"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"
    implementation "androidx.fragment:fragment-ktx:1.5.5"
    implementation "com.google.dagger:hilt-android:2.44.2"

    kapt "androidx.hilt:hilt-compiler:1.0.0"
    kapt "com.google.dagger:hilt-android-compiler:2.44.2"

    kapt "androidx.room:room-compiler:$room_version"

    implementation "io.coil-kt:coil:$coil_version"
    implementation "io.coil-kt:coil-compose:$coil_version"

    // Architectural Components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

    // Coroutine Lifecycle Scopes
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
    // ktor
    implementation "io.ktor:ktor-client-okhttp:1.6.5"
    implementation "io.ktor:ktor-client-json:1.6.5"
    implementation "io.ktor:ktor-client-logging-jvm:1.6.5"
    implementation "io.ktor:ktor-client-serialization-jvm:1.6.5"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"
    implementation "androidx.activity:activity-ktx:1.5.1"

    implementation "androidx.core:core-splashscreen:$splash_version"
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

And this is my simple test file ->

import org.junit.Assert
import org.junit.Test

class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        Assert.assertEquals(4, 2 + 2)
    }
}

The result i´m getting is this ->

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebugUnitTest'.
> There were failing tests. See the report at: file:///Users/develop/CineView4/app/build/reports/tests/testDebugUnitTest/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1s```

Where from the link above I got this error message -> ```org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 5.
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:63)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy2.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:135)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.junit.platform.commons.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
    at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:296)
    at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:48)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:105)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:75)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:97)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
    ... 25 more

I dont know what i´m doing wrong...if a take this in a new project it works just fine...I´m doing something wring in the gradle file?

Thanks!

So, I was able to fixed it. I compared the gradle code from my project with a new one and remove some unnecessary implementations. This is my new gradle ->

implementation project(':base')
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose"
implementation "androidx.activity:activity-compose:1.4.0"
implementation "androidx.compose.material:material:$compose"
implementation "androidx.compose.ui:ui-tooling:$compose"
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.0'
implementation "com.google.code.gson:gson:$gson_version"
implementation "androidx.fragment:fragment-ktx:1.5.5"
implementation "androidx.room:room-ktx:$room_version"
implementation "com.google.dagger:hilt-android:2.44.2"

kapt "androidx.hilt:hilt-compiler:1.0.0"
kapt "com.google.dagger:hilt-android-compiler:2.44.2"

kapt "androidx.room:room-compiler:$room_version"

implementation "io.coil-kt:coil:$coil_version"
implementation "io.coil-kt:coil-compose:$coil_version"

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
implementation "io.ktor:ktor-client-okhttp:1.6.5"
implementation "io.ktor:ktor-client-json:1.6.5"
implementation "io.ktor:ktor-client-logging-jvm:1.6.5"
implementation "io.ktor:ktor-client-serialization-jvm:1.6.5"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"
implementation "androidx.activity:activity-ktx:1.5.1"

implementation "androidx.core:core-splashscreen:$splash_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM