未解决的AndroidX引用问题:ActivityTestRule

31

我正在尝试使用AndroidX Espresso库通过仪器测试来测试我的用户界面。 在我的grade文件中,我有:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "it.zehus.mybike"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        android.defaultConfig.manifestPlaceholders = ['appAuthRedirectScheme': 'net.openid.appauthdemo']
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    def lifecycle_version = "2.0.0"
    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

    // room persistence library
    def room_version = "2.1.0-alpha04"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
    // optional - Coroutines support for Room
    implementation "androidx.room:room-coroutines:$room_version"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'

    // Espresso
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    implementation project(path: ':remotemanager')
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.21"

    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    // required if you want to use Mockito for unit tests
    testImplementation 'org.mockito:mockito-core:2.24.5'
    // required if you want to use Mockito for Android tests
    androidTestImplementation 'org.mockito:mockito-android:2.24.5'
    // Bluetooth sdk
    implementation project(path: ':bluetoothmanager')
    // Custom views
    implementation project(path: ':customviews')


}

根据文档所述,我试图在我的测试中导入ActivityTestRule类,但是引用未解决。

import androidx.test.filters.LargeTest
import androidx.test.runner.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
// unresolved reference here
import androidx.test.rule.ActivityTestRule
import it.zehus.mybike.ui.ride.RideActivity


/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
// deprecated here
@RunWith(AndroidJUnit4::class)
@LargeTest
class FragmentDashboardUiTest {
    @get:Rule // unresolved reference here
    val activityRule = ActivityTestRule(RideActivity::class.java)

    @Test
    fun myClassMethod_ReturnsTrue() {  }
}

我是不是做错了什么,还是AndroidX测试库本身存在问题?


2
https://dev59.com/SlQJ5IYBdhLWcg3w8anH#52776938 - Artem Botnev
请发布您的 build.gradle 文件。 - AndroidEnthusiast
@AndroidEnthusiast Gradle已更新! - Nicola Gallazzi
@ArtemBotnev 谢谢你的链接,它有助于解决 AndroidJUnit4 废弃的问题。 - Nicola Gallazzi
2个回答

58

我从这个文档页面了解到,在AndroidX中,类ActivityTestRule位于androidx.test.rule下。为了导入该包,我只需添加:

 androidTestImplementation 'androidx.test:rules:1.2.0'

致我的毕业论文。

总结一下,我的毕业论文现在包括:

// Espresso
// Core library
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

6
对于未来的读者,您可以通过访问https://maven.google.com/web/index.html并搜索包名称来找到任何给定软件包的最新版本。在这种情况下,搜索`android.test`并展开`rules`子包。 - donturner

2

这个类位于包:androidx.test.ext.junit.rules;

所以显然最重要的依赖关系是:

androidTestImplementation 'androidx.test.ext:junit:1.1.2'

我想知道为什么其他答案得到了如此大的提升,但它对我完全不起作用。

希望这能帮助像我一样的人。


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