Running Android Espresso Test errors with You need to use a Theme.AppCompat theme (or descendant) with this activity

Viewed 2351

Hi i'm trying to run an Espresso test but it fails and I'm sure what the problem is or what I am doing wrong.

When I run the tests I get the following in my console

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)

it says it fails because I need to use a Theme.AppCompat theme (or descendant) with this activity and shows in the stacktrace line 54 which is setContentView(R.layout.activity_login);

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

I have set the theme in the application and even made sure by setting the theme on the activity

   <application android:theme="@style/AppTheme">

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

and what my themes parent is "Theme.AppCompat"

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

So I am a bit confused as to why it fails on this when I am using a theme descending from Theme.AppCompat

here is also my activity extending from AppCompactActivity

public class LoginActivity extends AppCompatActivity 

my dependecies in my Gradle for androidTest are

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'

and this is the test I am trying to run

@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")));
    }

}

Maybe there is something bluntly obvious I am doing wrong.

Any help would be appreciated.

btw the app works fine when running it

3 Answers

If you don't want to mess with your theme setup or your Manifest then there's a simpler solution.

Set the theme on runtime, like so:

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