有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android测试给出了一个非法状态例外:未注册任何检测

我在谷歌教程之后编写了我的第一个安卓测试,当然,我遇到了错误:

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

我的代码:

package com.mikk.expressotest;

import 安卓.support.test.filters.LargeTest;
import 安卓.support.test.runner.AndroidJUnit4;

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

import 安卓x.test.core.app.ActivityScenario;

import static 安卓.support.test.espresso.Espresso.onView;
import static 安卓.support.test.espresso.action.ViewActions.click;
import static 安卓.support.test.espresso.assertion.ViewAssertions.matches;
import static 安卓.support.test.espresso.matcher.ViewMatchers.withId;
import static 安卓.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {

    @Before
    public void setUp() {
        ActivityScenario.launch(MainActivity.class);
    }

    @Test
    public void changeText_showHi() {
        onView(withId(R.id.btnOne)).perform(click());
        onView(withId(R.id.tvOne)).check(matches(withText(R.string.hi)));
    }
}

我还没有在堆栈中找到解决方案
如何修复它


共 (1) 个答案

  1. # 1 楼答案

    如果使用robolectric 4.0+,则需要使用androidx依赖项,例如

    dependencies {
        testImplementation "androidx.test.ext:junit:1.0.0"
        testImplementation "androidx.test:rules:1.1.0"
    }
    

    而不是安卓支持的