代码之家  ›  专栏  ›  技术社区  ›  George Udosen

无法在android应用程序中运行测试

  •  0
  • George Udosen  · 技术社区  · 7 年前

    我正试着为我的android应用程序运行一个工具测试,但不幸的是,我一直得到 java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

    package com.example.georgek.notekeeper;
    
    import android.support.test.runner.AndroidJUnit4;
    
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import androidx.test.espresso.ViewInteraction;
    import androidx.test.rule.ActivityTestRule;
    import androidx.test.runner.AndroidJUnitRunner;
    
    import static androidx.test.espresso.Espresso.onView;
    import static androidx.test.espresso.action.ViewActions.click;
    import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
    import static androidx.test.espresso.action.ViewActions.typeText;
    import static androidx.test.espresso.matcher.ViewMatchers.*;
    import static org.junit.Assert.*;
    
    @RunWith(AndroidJUnit4.class)
    public class NoteCreationTest extends AndroidJUnitRunner {
    
        @Rule
        public ActivityTestRule<NoteListActivity> mNoteListActivityActivityTestRule =
                new ActivityTestRule<>(NoteListActivity.class);
    
        @Test
        public void createNewNote() {
    //        ViewInteraction fabNewNote = onView(withId(R.id.fab));
    //        fabNewNote.perform(click());
            onView(withId(R.id.fab)).perform(click());
            onView(withId(R.id.text_note_title)).perform(typeText("Test note title"));
            onView(withId(R.id.text_note_text)).perform(typeText("This is the body of our test note"),
                    closeSoftKeyboard());
        }
    
    
    }
    

    还有我的gradle档案:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.example.georgek.notekeeper"
            minSdkVersion 24
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:support-v4:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:design:28.0.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
        androidTestImplementation 'androidx.test:runner:1.1.0'
        androidTestImplementation 'androidx.test:rules:1.1.0'
        androidTestImplementation 'com.android.support:support-annotations:28.0.0'
        androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    
    }
    

    java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
    at androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50)
    at androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101)
    at androidx.test.rule.ActivityTestRule.<init>(ActivityTestRule.java:144)
    at androidx.test.rule.ActivityTestRule.<init>(ActivityTestRule.java:120)
    at androidx.test.rule.ActivityTestRule.<init>(ActivityTestRule.java:103)
    at com.example.georgek.notekeeper.NoteCreationTest.<init>(NoteCreationTest.java:23)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
    

    注意,我已经看到了这些问题 this this 两个都没有帮助我的处境。

    2 回复  |  直到 7 年前
        1
  •  1
  •   The Dreams Wind    7 年前

    看来你正在使用的教程有点过时了。目前,AndroidX测试库迁移到 android.support.test . 您需要更换gradle中的测试运行程序以使用AndroidX包:

    
    defaultConfig {
        applicationId "the.dreams.wind.insect"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    ...
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:support-v4:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:design:28.0.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
        androidTestImplementation 'androidx.test:runner:1.1.0'
        androidTestImplementation 'androidx.test:rules:1.1.0'
        androidTestImplementation 'com.android.support:support-annotations:28.0.0'
        androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    
    }
    

    或替换依赖项:

    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:support-v4:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:design:28.0.0'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test:rules:1.0.2'
        androidTestImplementation 'com.android.support:support-annotations:28.0.0'
        androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    }
        2
  •  1
  •   Martin Zeitler    7 年前

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

    添加 androidx.test.ext:junit 依赖关系:

    dependencies {
    
        androidTestImplementation "androidx.test:core:1.0.0"
        androidTestImplementation "androidx.test.ext:junit:1.0.0"
    
        androidTestImplementation "androidx.test:runner:1.1.0"
        androidTestImplementation "androidx.test:rules:1.1.0"
    
        ...
    }
    

    AndroidJUnit4