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

筅森魡賤 提交于 2019-12-01 15:02:21

I've just fixed same issue, and my workaround was this:

create a dummy Application and set theme inside onCreate():

public class TestApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(R.style.AppTheme); //or just R.style.Theme_AppCompat
    }
}

then inside the test you specify the application class in config:

@RunWith(RobolectricTestRunner.class)
@Config(application = TestApplication.class)
public class YourActivityTest {
    //tests
}

Try this: Change your Android Manifest file to this:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat" >
    <activity
        android:name=".ui.MainActivity"
        android:label="@string/title">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
Derek Fung

It is a bug with Robotelectric, here is a workaround:

Android Lolipop Appcompat problems running with Robolectric

You may also try to upgrade to Robotelectric 3.0 to check if this is fixed.

You should use RobolectricGradleTestRunner instead.

import org.robolectric.RobolectricGradleTestRunner;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = Config.NONE)
public class MainActivityRoboTest {

It solves my problem.

whalemare

Just use @RunWith(RobolectricTestRunner.class) and be happy

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

    @Test
    public void myTest() throws Exception {
        assertEquals(true, true);
    }
}

See more info here: http://robolectric.org/getting-started/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!