当前位置: 首页 > 知识库问答 >
问题:

无法运行Android浓缩咖啡测试。该项目使用Hilt依赖注入和导航组件

裴甫
2023-03-14

我正在尝试运行下面的浓缩咖啡测试。我在我的项目中设置了Android Hilt依赖注入,它在应用程序中运行良好,但在运行UI测试时面临问题。

我也有一个项目库,它也在使用Hilt。

Process: com.muddassir.myapp, PID: 15570
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/internal/platform/tracker/UsageTracker;
        at java.lang.Class.newInstance(Native Method)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6952)
        at android.app.ActivityThread.access$1500(ActivityThread.java:272)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2055)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8019)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.internal.platform.tracker.UsageTracker" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.muddassir.myapp.test-sXVdfWq9tpbgNg9sbalPuAase.apk", zip file "/data/app/com.cs.stackoverflow-hHsBjEoBRyEmFRYwawGgqA==/base.apk"],nativeLibraryDirectories=[/data/app/com.cs.stackoverflow.test-sXVdfWq9tpbgNg9sbalPuA==/lib/arm, /data/app/com.cs.stackoverflow-hHsBjEoBRyEmFRYwawGgqA==/lib/arm, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
package com.muddassir.myapp.ui.activity


import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.cs.stackoverflow.R
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.internal.runners.JUnit4ClassRunner
import org.junit.runner.RunWith

@LargeTest
@HiltAndroidTest
@RunWith(AndroidJUnit4ClassRunner::class)
class ParameterTest {
    @get:Rule()
    var hiltRule = HiltAndroidRule(this)

    @get:Rule
    val activityRule = AndroidJUnit4ClassRunner(MainActivity::class.java)

    @Before
    fun init() {
        hiltRule.inject()
    }

    @Test
    fun parameterTest() {
        val recyclerView = onView(
            allOf(
                withId(R.id.questionsRv),
                childAtPosition(
                    withId(R.id.swipeToRefreshL),
                    0
                )
            )
        )
        recyclerView.perform(actionOnItemAtPosition<ViewHolder>(0, click()))
    }

    private fun childAtPosition(
        parentMatcher: Matcher<View>, position: Int
    ): Matcher<View> {

        return object : TypeSafeMatcher<View>() {
            override fun describeTo(description: Description) {
                description.appendText("Child at position $position in parent ")
                parentMatcher.describeTo(description)
            }

            public override fun matchesSafely(view: View): Boolean {
                val parent = view.parent
                return parent is ViewGroup && parentMatcher.matches(parent)
                        && view == parent.getChildAt(position)
            }
        }
    }
}

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "com.cs.myapp_lib.HiltTestRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled 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'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'

    def test_version = "1.3.1-alpha03"


    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'

    // Retrofit
    def retrofit = "2.9.0"
    api "com.squareup.retrofit2:retrofit:$retrofit"
    api "com.squareup.retrofit2:converter-gson:$retrofit"
    api "com.squareup.retrofit2:converter-scalars:$retrofit"
    api "com.squareup.retrofit2:adapter-rxjava2:$retrofit"

    api 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2'

    // Utilities
    api 'com.github.muddassir235:kmacros:1.10'

    def room = "2.2.6"
    api "androidx.room:room-runtime:$room"
    api "androidx.room:room-ktx:$room"
    kapt "androidx.room:room-compiler:$room"

    // Dependency Injection through HILT
    def hilt_version = '2.31.2-alpha'
    api "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"


    // ...with Kotlin.
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"

    def hilt_view_models = '1.0.0-alpha03'
    api "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_view_models"
    kapt "androidx.hilt:hilt-compiler:$hilt_view_models"

    // Libraries for tests

    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"

    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.3.1-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-web:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.4.0-alpha03'

    androidTestImplementation 'androidx.test:runner:1.3.1-alpha03'
    androidTestImplementation "androidx.test:core-ktx:$test_version"
    androidTestImplementation 'androidx.test:rules:1.3.1-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0-alpha03'
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
}
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.cs.stackoverflow"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "com.muddassir.myapp.HiltTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled 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'
    }
}

dependencies {
    implementation project(':stackoverflow_lib')

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'


    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'


    // Dependency Injection through HILT
    def hilt_version = '2.31.2-alpha'
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"



    // ...with Kotlin.
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
    // ...with Java.
//    androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'


    def hilt_view_models = '1.0.0-alpha03'
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_view_models"
    kapt "androidx.hilt:hilt-compiler:$hilt_view_models"

    // Libraries for tests

    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"

    def fragment_version = '1.3.0-rc02'
    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"

    def test_version = "1.3.1-alpha03"

    def nav_version = '2.3.3'

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-web:3.4.0-alpha03'
    androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.4.0-alpha03'

    androidTestImplementation 'androidx.test:runner:1.3.1-alpha03'
    androidTestImplementation 'androidx.test:rules:1.3.1-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0-alpha03'
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
    androidTestImplementation "androidx.test:core-ktx:$test_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.30"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

共有1个答案

仉宪
2023-03-14

使用稳定版本的测试运行器和规则1.3.01.3.1-alpha02

implementation "androidx.test:runner:1.3.0"
implementation "androidx.test:rules:1.3.0"
 类似资料:
  • 我有一个MainMenu活动,它从文件中获取数据并显示出来。我希望数据在每次浓缩咖啡测试开始时被删除,并且在测试之间不会持续。 我尝试了以下方法: 但是,它不会删除文件。我认为上下文可能不正确。有没有办法在浓缩咖啡测试开始时清除内部存储? 这些文件是。ser '文件。

  • 浓缩咖啡测试很烦人,因为像这样的代码 给出如下错误

  • 我在做Espresso测试时遇到了一个问题,我知道Espresso不能处理动画,所以我在下面做了。-禁用我的测试设备窗口动画,过渡动画和动画师持续时间比例都设置为关闭(这不起作用)-然后我试图在我的代码中添加一个标志(如。espresso_testing=true。如果为true,我的代码将跳过调用所有startAnimation()函数调用。--->这很管用。然而,在编写espresso测试用例

  • 我很难说服新的Android构建系统运行测试。在运行测试时,它给出了错误,这在其他问题中已经讨论过,但其中没有任何问题能够解决我的问题。 我已经将它剥离下来,这样我的测试包就完全不依赖于主包(),但仍然存在启动活动的问题。 我的测试活动: 和我的测试类: 建筑的相关部分。等级: 我获得的完整堆栈跟踪是: 我没有包括我的,因为我读到的所有内容都表明,我不需要为添加意图,但是我无论如何都试图这样做,结

  • 我试着用“浓缩咖啡”写简单的测试 但我有个错误: 我正在尝试不同的框架进行测试,对我来说是最好的,但如果有人能帮助修复这个错误,我将非常感激