Espresso:IllegalStateException:未注册生命周期监视器

时间:2017-01-03 10:52:49

标签: android android-espresso

我无法使用espresso-2.2.2执行测试用例,它会抛出异常。我做了研究来解决错误,但没有找到解决方案。请帮我解决这个问题。

  

java.lang.IllegalStateException:没有注册生命周期监视器!您是否在一个记录生命周期监视器的Instrumentation下运行?   在android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry.getInstance(ActivityLifecycleMonitorRegistry.java:44)   在android.support.test.espresso.base.BaseLayerModule.provideLifecycleMonitor(BaseLayerModule.java:52)   在android.support.test.espresso.base.BaseLayerModule_ProvideLifecycleMonitorFactory.get(BaseLayerModule_ProvideLifecycleMonitorFactory.java:18)

Gradle依赖

dependencies {
    compile project(':domain')
    compile project(':data')

    compile "com.squareup.leakcanary:leakcanary-android:1.3.1"
    apt "com.google.dagger:dagger-compiler:2.0.2"
    compile "com.android.support:appcompat-v7:23.4.0"
    compile "com.google.dagger:dagger:2.0.2"
    compile "com.jakewharton:butterknife:7.0.1"
    compile "io.reactivex:rxjava:1.0.14"
    compile "com.android.support:design:23.4.0"
    compile "io.reactivex:rxandroid:1.0.1"
    provided "javax.annotation:jsr250-api:1.0"
    compile "com.android.support:support-annotations:21.0.3"
    compile 'com.android.support:multidex:1.0.1'

    androidTestCompile "org.mockito:mockito-core:1.10.19"
    androidTestCompile "com.google.dexmaker:dexmaker:1.2"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
    androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.2"
    androidTestCompile "com.android.support.test:testing-support-lib:0.1"
    androidTestCompile "com.android.support.test:runner:0.5"
    androidTestCompile "com.android.support.test:rules:0.5"
}

在app level gradle中定义的自定义测试运行器

android {
    defaultConfig {
        testInstrumentationRunner "com.myapp.presentation.TestRunner"
        multiDexEnabled true
    }
}

自定义测试运行器在这里     包com.myapp.presentation;

public class TestRunner extends AndroidJUnitRunner
{
    @Override
    public void onCreate(Bundle arguments)
    {
        MultiDex.install(getTargetContext());
        super.onCreate(arguments);
    }
}

这是我的测试类

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

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

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ProductDetailsActivityTest {
    @Rule
    public ActivityTestRule<ProductDetailsActivity> mActivityRule = new ActivityTestRule<>(ProductDetailsActivity.class);

    @Test
    public void listGoesOverTheFold() {
        onView(withId(R.id.view_pager)).check(matches(isDisplayed()));
    }
}

1 个答案:

答案 0 :(得分:1)

最后,我在检查https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate时找到了解决方案。

问题是由于依赖

  

androidTestCompile&#34; com.android.support.test:testing-support-lib:0.1&#34;

我已删除此依赖项,现在我可以使用Espresso-v2.2.2执行测试用例