未解决的引用:testing

35

我想在我的安卓项目中编写简单的集成测试,完全使用Kotlin语言编写。

问题是测试甚至无法启动,并显示以下错误:

Error:(4, 36) Unresolved reference: testing
Error:(18, 52) Unresolved reference: InstantTaskExecutorRule
Error:Execution failed for task ':app:kaptGenerateStubsDebugAndroidTestKotlin'.
> Compilation error. See log for more details
我已经尝试在谷歌上搜索这个问题,但没有成功。
我已经尝试了以下步骤:
1. 检查包含InstantTaskExecutorRule的库是否已安装,并且我可以进入此包中(是的,我可以)。 2. 检查我的测试是否放置在正确的文件夹中(是的,它在androidTest中)。 3. 检查我是否正确启动了我的测试(可能是的,我通过右键单击包pkgName(androidTest),然后“运行...”来启动测试)。
我还尝试将源目录从Java重命名为Kotlin,并设置适当的值到sourceSets,但由于没有成功而将其改回,并认为这不是原因。
重要提醒:
如果我注释掉import android.arch.core.executor.testing.InstantTaskExecutorRule这一行以及与InstantTaskExecutorRule相关的所有代码(意味着整个测试逻辑为空),并放置一个简单的assert,那么一切都正常。
但我想使用这个特定的InstantTaskExecutorRule,并想知道问题实际上是什么以及如何解决它,或者至少知道我应该去哪里查找和什么。
import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import com.myapp.storage.base.AppDataBase


@RunWith(AndroidJUnit4::class)
class UserDaoTest{
    @JvmField @Rule val instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var db: AppDataBase

    @Before
    @Throws(Exception::class)
    fun setUp(){
        db = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDataBase::class.java)
                .allowMainThreadQueries()
                .build()
    }

    @After
    fun closeDB(){
        db.close()
    }

    @Test
    fun getUsersWhenNoUserInserted(){
        db.userDao().allUsers()
                .test().assertNoValues()
    }

}
4个回答

39

根据官方谷歌文档,我们应该以这种方式添加我们的测试助手来支持组件架构(LiveData):

// Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.0"

至少对我来说,它根本不起作用。(请参见上面的问题)

实际上应该是这样的:

// Test helpers for LiveData
androidTestImplementation "android.arch.core:core-testing:1.1.0"

现在一切都正常运作!


1
我和 @vendettacore 一样遇到了同样的问题,当在 JUnit 测试中的 test 目录中使用 import androidx.test.platform.app.InstrumentationRegistry 时。错误是“_未解决的引用:test_”。 - AdamHurwitz
1
这里是我上面关于 import 问题的 解决方案。它与库依赖项的添加方式有关。 - AdamHurwitz
2
我认为这取决于测试目录(仪器化或非仪器化)需要依赖项,因此它可能是testImplementation或androidTestImplementation。这意味着你可能需要两者都。有人可以确认或否认吗? - Quentin vk
我为所有测试依赖项都做了这个,现在错误已经消失了。 - GAMA
@Lawnio提到的是对我有用并且更有意义的。我一直在使用testImplementation,同时将实际测试放在与仪器测试相对应的目录中。将测试移动到正确的文件夹中解决了这个问题。 - Miguel Lasa
显示剩余2条评论

9

我也遇到了同样的问题,原因是我导入了更高版本的junit,4.13-beta-3。降级到4.12版本后,一切正常。

testImplementation "junit:junit:4.12"

希望这对其他人有所帮助。


1
我也遇到了这个问题,当我将JUnit从v4.13回退到4.12时,一切都正常了。 - anticafe
1
如果您想使用4.13版本,请参见https://dev59.com/pbjna4cB1Zd3GeqP-nFf#59715907。 - Romain Barbier
非常感谢Paul - 你的答案让我今天少很多抓狂! - Andrew Ebling

0
我不得不添加以下内容来解决在一个运行测试的KMP项目中出现的错误。
dependencies {
    implementation(kotlin("test"))
    implementation(kotlin("test-junit"))
}

这在gradle文件的根目录中,而不是测试项目中。


0

对我来说,具体问题与“Unresolved reference: InstantTaskExecutorRule”有关。我通过在依赖项括号中升级解决了这个问题。

    testImplementation "androidx.arch.core:core-testing:2.1.0"

从2.0.0到2.1.0 再见


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