Robolectric 3.3 与 Theme.AppCompat

Robolectric 3.3 with Theme.AppCompat

我正在使用 Robolectric 3.3com.android.support:appcompat-v7:23.4.0
测试 AppCompatActivity 在我得到之后:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我创建了以下测试运行程序:

public class ActivityTestRunner extends RobolectricTestRunner {

    public ActivityTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config)
    {
        return new AppManifest(Fs.fileFromPath(config.manifest()),
                Fs.fileFromPath(config.resourceDir()),
                Fs.fileFromPath(config.assetDir()));
    }

    private class AppManifest extends AndroidManifest
    {
        private static final String THEME = "@style/Theme.AppCompat";


        public AppManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) {
            super(androidManifestFile, resDirectory, assetsDirectory);
        }

        public AppManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory, String overridePackageName) {
            super(androidManifestFile, resDirectory, assetsDirectory, overridePackageName);
        }

        @Override
        public String getThemeRef() {
            return THEME;
        }

        @Override
        public String getThemeRef(String activityClassName) {
            return THEME;
        }
    }
}

然后我得到:

java.lang.NullPointerException
    at org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals(ThemeStyleSet.java:67)
    at org.robolectric.res.ThemeStyleSet.apply(ThemeStyleSet.java:29)
    at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:595)
    at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:588)
    at android.content.res.AssetManager.applyThemeStyle(AssetManager.java)
    at android.content.res.Resources$Theme.applyStyle(Resources.java:1104)
    at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:132)
    at android.app.Activity.onApplyThemeResource(Activity.java:3307)
    at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:144)
    at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:89)
    at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90)
    at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:89)
    at org.robolectric.shadows.CoreShadowsAdapter.setThemeFromManifest(CoreShadowsAdapter.java:30)
    at org.robolectric.android.controller.ActivityController.attach(ActivityController.java:84)
    at org.robolectric.android.controller.ActivityController.of(ActivityController.java:34)
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:93)
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:89)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:97)
    at com.lacoon.components.activities.MainActivityTest.setUp(MainActivityTest.java:35)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:209)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:63)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

风格:

<style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:textColor">@color/white</item>
        <item name="colorPrimary">@color/main_background</item>
        <item name="colorPrimaryDark">@color/main_background</item>
        <item name="android:textViewStyle">@style/RobotoLightTextViewStyle</item>"
        <item name="colorAccent">@color/check_point_pink</item>
        <item name="colorControlNormal">#9a9a9a</item>
        <item name="colorControlActivated">@color/check_point_pink</item>
        <!--<item name="editTextStyle">@style/welcome_editbox</item>-->
        <!--<item name="alertDialogStyle">@style/dialog_theme</item>-->
    </style>

onCreate:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);
        getMainScreenFragment();
        mToolbar = (Toolbar) findViewById(toolbar);
        setUpToolBar();
    }

如何实现?

您遇到此问题的原因是因为您尝试应用对话框主题的 activity 正在扩展 ActionBarActivity,这需要应用 AppCompat 主题。

更新:扩展 AppCompatActivity 也会有这个问题

在这种情况下,将 Java 继承从 ActionBarActivity 更改为 Activity 并将清单中的对话框主题保持原样,一个非 Theme.AppCompat 值

一般规则是,如果您希望您的代码支持旧版本的 Android,它应该有 AppCompat 主题并且 java 代码应该扩展 AppCompatActivity。如果您有一个不需要此支持的 activity,例如您只关心 Android 的最新版本和功能,您可以对其应用任何主题,但 java代码必须扩展普通的旧 Activity.

注意:当从 AppCompatActivity(或子类,ActionBarActivity)更改为 Activity 时,还必须将使用 "support" 的各种调用更改为相应调用没有 "support"。因此,不要调用 getSupportFragmentManager,而是调用 getFragmentManager。

只需删除您的自定义运行器并修复 Robolectric 测试的路径。查看操作方法 .

问题是我没有添加 @Config(constants = BuildConfig.class)
其中必须加在tutorial

对我来说,解决问题的方法是将以下内容添加到我的应用 gradle 文件

android {
   //OTHER CONFIG

   testOptions {
    unitTests {
        includeAndroidResources = true
    }
}


}