您需要使用 Theme.AppCompat 主题 -->在单元测试中 [英] You need to use a Theme.AppCompat theme --> in Unit Test

查看:42
本文介绍了您需要使用 Theme.AppCompat 主题 -->在单元测试中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个话题已经讨论过了,但这里有点不同:

I know this topic has been discussed but it is a little different here:

  • 应用运行良好
  • 错误仅在单元测试中显示
  • 主题很好(新创建的示例项目)

如何复制:

  1. 创建一个新项目 --> FullScreenActivity Android 4.3
  2. 创建单元测试
  3. 运行它

public class FullscreenActivityTest extends ActivityUnitTestCase<FullscreenActivity> {

public FullscreenActivityTest() {
    super(FullscreenActivity.class);
}

public void testStart() {
    startActivity(new Intent(getInstrumentation()
            .getTargetContext(), FullscreenActivity.class), null, null);
    Assert.assertNotNull(getActivity());
}

}

测试:

  1. Nexus 5 模拟器
  2. Nexus 6P 模拟器

每次都一样,应用程序运行良好.单元测试失败:

Each time the same, app works fine. Unit test fails with:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
        at com.sample.foobar.FullscreenActivity.onCreate(FullscreenActivity.java:88)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:163)

谢谢,

保罗

推荐答案

以下代码对我有用——添加到单元测试中:

The following code worked for me -- added to the unit test:

@Override
public void setUp(){
    ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
    setActivityContext(context);
}

另见:ActivityUnitTestCase 和 startActivity 与 ActionBarActivity

也可以

使用 ActivityInstrumentationTestCase2 而不是 ActivityUnitTestCase 也可以解决这个问题.

Use ActivityInstrumentationTestCase2 instead of ActivityUnitTestCase fixes the issue too.

此外,未调用活动的onPause".这与 ActivityUnitTestCase

In addition the "onPause" of the activity isn't called. Which is somehow odd with ActivityUnitTestCase

这篇关于您需要使用 Theme.AppCompat 主题 -->在单元测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆