24

Take a look at the official documentation. The section Include framework dependencies gives an example of how to set up a local unit testing to work with the environment android sdk. But if you do everything as in the example, the test does not start. I get an error instead

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

All attempts were made on a new project. Android Studio 3.3, gradle-4.10.1, build:gradle:3.3.0, Kotlin, and include Androidx artifacts.

Then added the following lines to the project with the specified configuration:

build.gradle

android {
    // ...
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

dependencies {
    // ...
    // Already exist
    testImplementation 'junit:junit:4.12'
    // Added this line
    testImplementation 'androidx.test:core:1.0.0'
}

And the test body itself:

package com.example.myapplication

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.Test

class ExampleUnitTest {

    val context = ApplicationProvider.getApplicationContext<Context>()

    @Test
    fun readStringFromContext_LocalizedString() {
        System.out.println(context.applicationInfo.packageName)
    }
}

What am I doing wrong?


apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.1.0-alpha03'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.test:core:1.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
maXp
  • 1,428
  • 1
  • 15
  • 23
  • where is your test file located inside test folder or androidTest folder? – Kaveri Jan 16 '19 at 17:49
  • @Kaveri I need local unit tests so the file is located in the test directory – maXp Jan 16 '19 at 18:38
  • 1
    but since you are using context in your test , should be using intrumentational unit test that go in androidTest folder. – Kaveri Jan 16 '19 at 18:43
  • 9
    @Kaveri Thanks, but no, I need local unit tests without running on an android device. If you carefully read the documentation on the link you will understand that this is possible by means of Robolectric. But for some reason, the presented example in the documentation does not work and it is not clear to me why. – maXp Jan 16 '19 at 19:06

1 Answers1

15

Update:

You should no longer encounter this error if you're using the latest gradle version.


I guess you need to include Robolectric dependency in your build.gradle and also specify test runner for your test:

@RunWith(RobolectricTestRunner.class)
class ExampleUnitTest {

After that it worked for me. I don't know why this info is not included in the Android documentation.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 7
    Thank! In the end, I did just that and I also don’t understand why this is not included in the documentation. – maXp Feb 19 '19 at 08:45
  • 1
    This is not in the documentation because Robolectric is only needed as a bridge between the Java and Android world, to give you access to Android-specific classes in "not-instrumented" tests (pure Junit tests, so to speak). If you're running instrumented tests (e.g. Espresso tests) they will have access to all the classes of the framework and therefore libraries like Robolectric are not needed at all. – Jorge Sep 06 '19 at 10:34
  • 4
    @Jorge See docs section: `If your tests interact with several Android framework dependencies, or interact with those dependencies in a complex way, use the Robolectric artifacts that AndroidX Test provides`. But the test runner annotation is missing, and that's strange. We are not talking about pure Junit tests in this case. We are not talking about instrumented tests either. – Vadim Kotov Sep 06 '19 at 10:50
  • Updated my answer, this test runner annotation is no longer needed now – Vadim Kotov Dec 04 '19 at 11:50
  • I am doing something really wrong here, and I don't know what, I am on latest gradle (6.5), Gradle plugin 4.1.1, I have on my dependencies ``` testImplementation "org.robolectric:robolectric:4.4" testImplementation 'androidx.test:core:1.3.0' ``` But I am still getting the same error – Alan Donizete Dec 22 '20 at 12:52
  • What version do you mean by the `latest gradle version`? Would be helpful for anybody wondering if they actually need to update or already use a version newer than what you referred to – arekolek Oct 21 '21 at 11:36
  • @arekolek Sorry, I do not have an answer. Maybe you can find something in the changelogs.. – Vadim Kotov Oct 26 '21 at 13:05
  • For some reason I no longer need the annotation, but still need the robolectric dependency. Are you able to explain this? – adrem7 Jan 12 '22 at 17:37
  • @adrem7 Because you need this artifact. See above: `If your tests interact with several Android framework dependencies, or interact with those dependencies in a complex way, use the Robolectric artifacts that AndroidX Test provides` – Vadim Kotov Jan 13 '22 at 10:41
  • @VadimKotov - I see. Is there no AndroidX dependency which replaces the Robolectric dependancy in its entirety then? I am attempting to migrate away from old Robolectric code and in doing so seem to have to have Robolectric and AndroidX dependencies side by side quite regularly. Is this to be expected? – adrem7 Jan 13 '22 at 16:04