当前位置: 首页 > 知识库问答 >
问题:

IllegalStateException:没有注册检测!必须在注册检测下运行

秦博延
2023-03-14

我一直在尝试使用Espresso执行一个简单的UI测试,但所有测试都失败了,但有一个相同的例外:

IllegalStateException:没有注册检测!必须在注册检测下运行

这里是使用eSpresso的初学者指南。我已经发现了类似的问题,但与我最相关的问题在这里没有得到回答--我想这是因为他们没有描绘出整个画面,所以这里是我的代码。我将只显示一个测试,因为它们都失败,错误完全相同:

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'
}
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设置文档操作,但我总是得到同样的错误:

测试运行到完成。

下面是我在espresso设置文档中描述的测试配置

共有1个答案

袁耀
2023-03-14

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

 类似资料:
  • A) 为什么当我尝试运行应用程序时会出现此错误? 我的崩溃日志错误 Java语言lang.RuntimeException:无法启动活动java。lang.IllegalStateException:未注册任何检测!必须在注册检测下运行。在android上。应用程序。ActivityThread。在android上执行LaunchActivity(ActivityThread.java:2947)

  • 本文向大家介绍Xshell5 注册码 ,Xftp5 注册码,Xmanager5注册码(已测),包括了Xshell5 注册码 ,Xftp5 注册码,Xmanager5注册码(已测)的使用技巧和注意事项,需要的朋友参考一下 Xmanager5 是全新标准的跨平台集成解决方案。它是一个一站式解决方案,这个软件包含有以下一些产品:Xshell5,Xftp5和Xlpd5。 Xshell Xshell是一个用

  • 问题内容: 是否可以检测PC何时注销。我需要开发一个应用程序,在PC注销之前,该应用程序将有关注销时间的文本文档写入文本。 问题答案: 对于.NET,请参见以下问题:[c#中是否有方法可以检测Windows关闭/注销并取消该操作(询问用户之后)http://codingdict.com/questions/159553)

  • 源代码 通过测试注册中心可以方便地在以太坊测试网络上测试ENS。测试注册中心通常部署在.test TLD上,它允许用户即时注册一个用于测试目的的域名,该域名在注册28天后自动过期。 注册一个域名 function register(bytes32 label, address owner) public; 1 注册一个其keccak256散列等于label的子域名,并将其所有者设置为owner。例

  • 嗨,我正在尝试使用JUNIT 5作为框架实现集成测试,我们只想在执行所有测试之前启动所有过程一次,并在执行所有测试时停止所有过程。 我发现没有简单的方法可以做到这一点,因为我们有很多测试类,而且BeforeAll和AfterAll方法对每个测试类都有效 我发现,如果我可以注册我自己的自定义听众,我也许可以做到这一点,但不知何故,它不起作用。 知道我们如何注册我们自己的监听器/任何其他方法来检测是否

  • 我有一个SpringBootTest,需要通过ApplicationEnvironmentPreparedEvent通知我创建一个不存在的数据库文件,因为我的应用程序数据库试图连接到它,但它不存在。 我是通过SpringApplicationBuilder实现的,但是在JUnit中,我没有访问这个生成器。这是我当前的主代码: 当准备就绪时,如何通知我在数据源配置之前读取JDBC URL并为测试创建