java - Android 测试给出 IllegalStateException : No instrumentation registered

标签 java android testing

我在谷歌教程之后写了我的第一个 android 测试,当然我得到了错误:

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

我的代码:

package com.mikk.expressotest;

import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;

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

import androidx.test.core.app.ActivityScenario;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.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)));
    }
}

我还没有在堆栈中找到解决方案。
如何解决?

最佳答案

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

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

instread 的 android 支持。

关于java - Android 测试给出 IllegalStateException : No instrumentation registered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52888861/

相关文章:

java - FreeMarker - 从毫秒中删除逗号

java - Android:在模拟器上为没有 Google Play 服务的用户测试逻辑

java - 如何使用camera2修改帧率?

android - 如何将 listview 的 onClick 事件传递给父 View

android - 禁用 ContentProvider URI 上的通知

html - 如何通过有条件地排除父节点的前一个兄弟节点来选择子节点

spring - 如何在过去设置@CreatedDate(用于测试)

java - Golang enum 可以像 Java 的 enum 一样做同样的行为吗?

java - 缺少 "Run as JUnit Test"

java - Spring - 如何在声明式事务管理回滚期间收集信息