gpt4 book ai didi

java - 无法在 Android 应用程序中运行测试

转载 作者:行者123 更新时间:2023-11-28 21:07:29 26 4
gpt4 key购买 nike

我正在尝试按照教程为我的 android 应用程序运行仪器测试,但不幸的是,我一直收到 java.lang.IllegalStateException:未注册仪器!必须在注册检测下运行。 错误。我已经在 gradle 文件中添加了所需的依赖项,但仍然无法运行。这是我的测试文件:

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)

注意我已经看到问题thisthis两者都对我的情况没有帮助。

最佳答案

您使用的教程似乎有点过时了。目前 AndroidX 测试库已迁移到 android.support.test。您需要在 gradle 中替换您的测试运行程序才能使用 AndroidX 包:


defaultConfig {
applicationId "the.dreams.wind.insect"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
<b>testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"</b>
}
...

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'
<b><s>androidTestImplementation 'com.android.support.test:runner:1.0.2'</s></b>
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'
<b><s>androidTestImplementation 'androidx.test:runner:1.1.0'</s></b>
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
<b>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'</b>
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
}

关于java - 无法在 Android 应用程序中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com