[英]Unable to run test in android application
I am trying to run an Instrumentation Test for my android application followiwng a tutorial but unfortunately I keep getting the java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
我正在尝试按照教程学习我的android应用程序的工具测试,但不幸的是,我一直在获取java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
error. 错误。 I have added the required dependencies in the gradle file but still can't get it to run. 我已经在gradle文件中添加了所需的依赖项,但仍然无法使其运行。 Here is my test file: 这是我的测试文件:
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());
}
}
And my gradle file: 和我的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'
}
And this is the error message I am getting: 这是我收到的错误消息:
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)
Note I have seen the questions this and this and both have not helped my situation. 请注意,我已经看到了这个和这个问题,并且都没有帮助我解决问题。
It seems that the tutorial you are using is a little bit outdated. 您正在使用的教程似乎有些过时了。 For now AndroidX test libs migrated to the android.support.test
. 目前,AndroidX测试库已迁移到android.support.test
。 You need either replace your test runner in gradle to use AndroidX package: 您需要替换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'
}
OR replace your dependencies: 或替换您的依赖项:
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'
}
define the runner: 定义跑步者:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
and add androidx.test.ext:junit
to the dependencies: 并添加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"
...
}
the dependency is required to replace deprecated class AndroidJUnit4
. 需要依赖项来替换不推荐使用的类AndroidJUnit4
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.