AndroidX:没有注册仪器!必须在注册仪器下运行 [英] AndroidX : No instrumentation registered! Must run under a registering instrumentation

查看:35
本文介绍了AndroidX:没有注册仪器!必须在注册仪器下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据上下文运行本地单元测试,并遵循本指南:https://developer.android.com/training/testing/unit-testing/local-unit-tests#kotlin 我这样设置我的项目(按照此链接:https://developer.android.com/training/testing/set-up-project ):

I'm trying to run a local unit test that depends on the context, and was following this guide: https://developer.android.com/training/testing/unit-testing/local-unit-tests#kotlin and I set up my project like this (following this link : https://developer.android.com/training/testing/set-up-project ):

build.gradle(app)

android {
compileSdkVersion 28
buildToolsVersion '27.0.3'
defaultConfig {
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 76
    versionName "2.6.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true

useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'

}
testOptions {
    unitTests.returnDefaultValues = true
    unitTests.all {
        // All the usual Gradle options.
        testLogging {
            events "passed", "skipped", "failed", "standardOut", "standardError"
            outputs.upToDateWhen { false }
            showStandardStreams = true
        }
    }
    unitTests.includeAndroidResources = true

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

androidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion", {
    exclude group: 'com.android.support', module: 'support-annotations'
})
// Espresso UI Testing dependencies
implementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"

testImplementation 'androidx.test:core:1.0.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Espresso Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
    implementation 'androidx.multidex:multidex:2.0.0'
}

我的 espresso_version 是 espressoVersion = '3.1.0'

My espresso_version is espressoVersion = '3.1.0'

位于 module-name/src/test/java/ 中的测试如下所示:

My test that is located in module-name/src/test/java/ looks like this:

    import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.instacart.library.truetime.TrueTime
import edu.mira.aula.shared.extensions.android.trueDateNow
import edu.mira.aula.shared.network.ConnectivityHelper
import kotlinx.coroutines.experimental.runBlocking
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.util.*
import java.util.concurrent.CountDownLatch

class TimeExtensionsUnitTest {
private lateinit var instrumentationCtx: Context

@Before
fun setup() {
    instrumentationCtx = ApplicationProvider.getApplicationContext<Context>()
}
 @Test
fun testTrueTimeValueReturnsIfInitialized() {
    if (ConnectivityHelper.isOnline(instrumentationCtx)) {
        runBlocking {
            val countDownLatch = CountDownLatch(1)
            TrueTime.build()
                    .withSharedPreferencesCache(instrumentationCtx)
                    .withConnectionTimeout(10000)
                    .initialize()
            countDownLatch.countDown()

            try {
                countDownLatch.await()
                val dateFromTrueTime = trueDateNow()
                val normalDate = Date()
                Assert.assertNotEquals(dateFromTrueTime, normalDate)
            } catch (e: InterruptedException) {
            }
        }
    }
}

每次我运行它时,它都会给我:

Everytime I run it, it gives me:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
 at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
  at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)

如果我将它作为仪器测试运行(更改包),它运行时不会出错.但我认为本指南正是为了能够使用诸如 Context 之类的 Android 框架类来运行单元测试.我什至尝试运行那个 class UnitTestSample 但发生了同样的错误.

If I run it as a Instrumental Test(changing the package) it runs without errors. But I thought that this guide was exactly to be able to run unit test using Android Framework classes such as Context. I even tried run that class UnitTestSample but the same error occurs.

我还从我的项目中删除了所有 android.support 依赖项

I also removed all android.support dependencies from my project

关于如何解决它的任何想法?

Any ideas on how to solve it?

推荐答案

更新

如果您使用的是最新的 gradle 版本,您应该不会再遇到此错误.

You should no longer encounter this error if youre using the latest gradle version.

我也遇到了这个问题.

如果您想迁移到 Robolectric 4.0 此处,建议添加gradle.properties 中的以下行.

If you look at migrating to Robolectric 4.0 here, it suggest to add the following line in your gradle.properties.

android.enableUnitTestBinaryResources=true

问题是,如果你把它添加到你的gradle.properties,它会输出这个警告:

The problem is that, if you add this you your gradle.properties, it will output this warning:

警告:选项设置'android.enableUnitTestBinaryResources=true' 是实验性的,不支持.

WARNING: The option setting 'android.enableUnitTestBinaryResources=true' is experimental and unsupported.

现在,如果您在此处查看 Robolectric 版本.您可以看到这是一个已知问题,他们指出

Now, if you look at Robolectric releases here. You could see that this is a known issue where they state that

Android Gradle 插件可能会报告以下警告,可以安全地忽略:警告:选项设置android.enableUnitTestBinaryResources=true"是实验性的且不受支持.Android Gradle 插件 3.4 将解决此问题.

Android Gradle Plugin may report the following warning, which may be safely ignored: WARNING: The option setting 'android.enableUnitTestBinaryResources=true' is experimental and unsupported.. Android Gradle Plugin 3.4 will resolve this issue.

我相信除非你能将你的 gradle 更新到 3.4.您将无法解决此问题.

I believe unless you could update you gradle to 3.4. You won't be able to solve this issue.

我所做的是将 Robolectric 4.0 作为依赖项包含进来.

What I did instead was to include Robolectric 4.0 as dependency.

testImplementation "org.robolectric:robolectric:4.0.2"

并用

@RunWith(RobolectricTestRunner::class)

这应该能让你的测试工作.

This should make your test work.

现在,当您运行测试时,您会注意到 Robolectric 将记录以下内容:

Now when you run the test, you'll notice that Robolectric will log the following:

[Robolectric] 注意:旧资源模式已弃用;看http://robolectric.org/migrating/#migrating-to-40>

[Robolectric] NOTICE: legacy resources mode is deprecated; see http://robolectric.org/migrating/#migrating-to-40

暂时忽略这一点,但一旦您可以更新您的 gradle,请迁移到新的 Robolectric 测试.

Ignore this for now but as soon as you could update your gradle, migrate to the new Robolectric testing.

这篇关于AndroidX:没有注册仪器!必须在注册仪器下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆