JUnit 本地测试 - 'Unresolved reference: test'

JUnit Local Test - 'Unresolved reference: test'

预计

androidx.test:core:1.2.0androidx.test.ext:truth:1.2.0com.google.truth:truth:0.44 等库导入 local JUnit 测试 class名为 ExampleUnitTest.kt.

观察到

错误

Unresolved reference: test

实施

ExampleUnitTest.kt

import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.google.common.truth.Truth.assertThat

解决方案

这似乎是 Android Testing Codelab sample app 中记录的问题。

在 Codelab 示例中 build.gradle 这称为已知问题。

// Once https://issuetracker.google.com/127986458 is fixed this can be testImplementation
implementation "androidx.test:core:$androidXTestCoreVersion"

我将应用程序的依赖项重构为 testImplementation 而不是 androidTestImplementation

testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test.ext:truth:1.2.0'
testImplementation 'com.google.truth:truth:0.44'

注意 - Google 文档 Set up project for AndroidX Test 应该更新以反映此问题,因为它目前指示开发人员使用 androidTestImplementation。我已提交文档问题来解决此问题。