运行 Android Espresso 测试错误 您需要在此活动中使用 Theme.AppCompat 主题(或后代)


嗨,我

正在尝试运行浓缩咖啡测试,但它失败了,我确定问题是什么或我做错了什么。

当我运行测试时,我在控制台中得到以下内容

Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

说它失败了,因为我需要在此活动中使用 Theme.AppCompat 主题(或后代),并显示在 setContentView(R.layout.activity_login) 的堆栈跟踪行 54 中;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

我已经在应用程序中设置了主题,甚至通过在活动上设置主题来确保

   <application android:theme="@style/AppTheme">
  <activity
            android:name=".Login.LoginActivity"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="adjustPan"></activity>

以及我的主题父级是"主题.AppCompat"

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

所以我有点困惑,当我使用来自 Theme.AppCompat 的主题时,为什么它会失败

这也是我从AppCompactActivity扩展的活动

public class LoginActivity extends AppCompatActivity 

我的 Gradle for androidTest 中的依赖项是

androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile  'com.android.support.test:runner:0.4.1'
androidTestCompile  'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'

这是我正在尝试运行的测试

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginNavigationForgotPasswordTest {
    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
    @Test
    public void testClick() {
        // Find Button and Click on it
        onView(withId(R.id.login_forgot_password_button)).perform(click());
        // Find TextView and verify the correct text that is displayed
        onView(withId(R.id.forgot_top_title)).check(matches(withText("Forgot Password")));
    }
}

也许我做错了什么。

任何帮助将不胜感激。

顺便说一句,该应用程序在运行时工作正常

不确定是什么修复了它,但我做了一些更改并设法获得了有效的设置。

  1. 更改了"应用主题"的名称。我想当 Gradle 构建它时,它与其他风格相同的库混淆了.xml,所以我把它重命名为更独特的名称。例如

以前:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

后:

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  1. 我还将浓缩咖啡更新到较新的版本,并确保测试 android 应用程序注释与应用程序注释相同。我还排除了一些模块,如此处所述 为什么添加浓缩咖啡会导致膨胀异常?

以前:

androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile  'com.android.support.test:runner:0.4.1'
androidTestCompile  'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'

后:

compile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
    androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.github.afollestad.material-dialogs', module: 'core'
        exclude group: 'com.github.afollestad.material-dialogs', module: 'commons'
        exclude group: 'com.github.traex.rippleeffect', module:'library'
        exclude group: 'com.balysv', module:'material-ripple'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile ('com.android.support.test:runner:0.5'){
    }
    androidTestCompile ('com.android.support.test:rules:0.4.1'){
    }
    androidTestCompile ('junit:junit:4.12')
    androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
    androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
    androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
    androidTestCompile 'org.mockito:mockito-core:1.10.19'

我还添加了以下决议策略

configurations.all{
    resolutionStrategy {
        force 'org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
    }
}

事后看来,我也可以通过这种方式添加支持注释

configurations.all{
    resolutionStrategy {
        force 'com.android.support:support-annotations:23.2.1','org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
    }
}

我遇到了同样的问题,但对我来说,解决方案只是在AndroidTest的清单中放置一个基于AppCompat的主题,就像我在Android应用程序清单中一样。对于我的项目,应用程序清单不用于浓缩咖啡测试,而是使用 AndroidTest 清单。

如果您不想弄乱主题设置或清单,那么有一个更简单的解决方案。

在运行时设置主题,如下所示:

@Before
  fun setUp() {
    context = InstrumentationRegistry.getInstrumentation().targetContext
    context.setTheme(R.style.Theme_AppCompat_Light)
  }

相关内容