AndroidX:未注册任何工具!必须在注册工具下运行 [英] AndroidX : No instrumentation registered! Must run under a registering instrumentation

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

问题描述

我正在尝试运行依赖于上下文的本地单元测试,并遵循了本指南: 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)

如果我将其作为仪器测试(更改程序包)运行,则运行不会出错. 但是我认为该指南完全能够使用Android框架类(例如Context)运行单元测试. 我什至尝试运行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'是实验性的,不受支持.AndroidGradle插件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天全站免登陆