Android测试给出了IllegalStateException:未注册任何检测

时间:2018-10-19 08:47:10

标签: java android testing

我写了我的第一个android测试,之后是Google教程,当然我遇到了错误:

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)));
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

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

了解Android支持人员。