java.lang.IllegalStateException : No instrumentation registered! 必须在注册工具下运行

标签 java android unit-testing junit android-espresso

我一直在尝试使用 Espresso 执行简单的 UI 测试,但我的所有测试都因相同的异常而失败:

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

这是使用 esspresso 的初学者指南 here .我已经找到了类似的问题,但与我最相关的问题没有得到解答 here - 我想这是因为他们没有画出全貌,所以这是我的代码。我只会展示一个测试,因为它们都以完全相同的错误失败:

build.gradle(Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "io.github.vinge1718.myrestaurants"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

dependencies {
    testImplementation "org.robolectric:robolectric:3.8"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

build.gradle(项目:MyRestaurant)

buildscript {
    
    repositories {
        google()
        jcenter()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

此处的更正是两个 测试。我不认为该错误与测试本身有任何关系,而是与配置有关 - 我同意纠正

(MainActivityInstrumentationTest.java)

package io.github.vinge1718.myrestaurants;

import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.rule.ActivityTestRule;

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.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
    private String mStringToBetyped;

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void initValidString() {
        // Specify a valid string.
        mStringToBetyped = "Portland";
    }

    @Test
    public void validateEditText(){
        onView(withId(R.id.locationEditText))
                .perform(typeText(mStringToBetyped), closeSoftKeyboard())
                .check(matches(withText(mStringToBetyped)));
    }

    @Test
    public void locationIsSentToRestaurantActivity(){
        String location = "Portland";
        onView(withId(R.id.locationEditText)).perform(typeText(location));
        onView(withId(R.id.findRestaurantsButton)).perform(click());
        onView(withId(R.id.locationTextView)).check(matches(withText("Here are all the Restaurants near " + location)));
    }
}

我已尝试按照此 Espresso 设置文档 here但我不断收到同样的错误:

Started running tests

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.(ActivityTestRule.java:144) at androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:120) at androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:103) at io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest.(MainActivityInstrumentationTest.java:25) 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)

Tests ran to completion.

这是我在 Espresso 设置中描述的测试配置 documentation :

最佳答案

我认为这是因为库 androidxcom.android.support.test 冲突。如果你想使用 jetpack,你必须将所有测试库转换为 androidx,如果你不想那样,只需删除你的 androidx 库并使用所有 com.android.support。测试。查看我在Android Instrumentation Testing: No instrumentation registered Error中的最新答案.希望对您有所帮助。

关于java.lang.IllegalStateException : No instrumentation registered! 必须在注册工具下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53021525/

相关文章:

java - JSF:AJAX 测试最佳实践

java - 使用目录创建 excel 文件 Spring boot java

android - 无法解析符号 '?selectableItemBackgroundBorderless'

包内资源的 Android 路径

java - 如何对具有95%依赖性的java项目进行单元测试?

java - Android:上传字符串到服务器

Java小程序AccessControlException问题

java - 如何使用类名获取HTML内容?

java - 创建 Double 来测试空检查时抛出空指针异常

unit-testing - 编写 unittest 并保存在单独的文件夹中