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

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


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}


I faced that issue when I wanted to start a fragment that had appBar in its layout.

Below approach worked for me.

        launchFragmentInContainer<ProfileSettingsFragment>(themeResId = R.style.AppTheme).moveToState(Lifecycle.State.RESUMED)


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>