37

I have a problem when testing an app which uses ActionBarActivity from android-support-v7-appcompat via Android JUnit test in Eclipse. When running in an emulator or device everything seems to work fine.

I tried using a mock application as in ActivityUnitTestCase and startActivity with ActionBarActivity and changed the parent theme in values-v11 etc. as suggested in ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat but it still does not work.

You need to use a Theme.AppCompat theme (or descendant) with this activity does not give an answer either, als the person asking the question neither had an Theme.AppCompat specified in his manifest (which I do), nor did he really want to extend ActionBarActivity (which I do). His solution was to simply extend Activity instead.

What am I doing wrong?

This is the error I get (Failure-Trace from the Junit-Window):

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at android.hello.HelloWorldActivity.onCreate(HelloWorldActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at android.hello.test.HelloWorldActivityTest.setUp(HelloWorldActivityTest.java:26)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

HelloWorldActivity.java

package android.hello;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends ActionBarActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(android.hello.R.id.tv);
        tv.setText("Hello, Android");

    }
}

HelloWorldApplication.java

package android.hello;

import android.app.Application;
import android.util.Log;

public class HelloWorldApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(R.style.Theme_AppCompat);
    }
}

Hello World Manifest:

...
<activity
    android:name=".HelloWorldActivity"
    android:label="@string/app_name" 
    android:theme="@style/Theme.AppCompat">
    ...
</activity>
....

From the test package:

HelloWorldActivityTest.java

package android.hello.test;

import android.hello.HelloWorldActivity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.TextView;

public class HelloWorldActivityTest extends ActivityUnitTestCase<HelloWorldActivity> {

    HelloWorldActivity helloWorldActivity; 
    TextView textView;

    public HelloWorldActivityTest() {
        super(HelloWorldActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Starts the MainActivity of ScanMe
        startActivity(new Intent(getInstrumentation().getTargetContext(),       HelloWorldActivity.class), null, null);

        // Reference to the MainActivity of ScanMe
        helloWorldActivity = (HelloWorldActivity)getActivity();

        // Reference to the code input-TextEdit of the MainActivity of ScanMe
        textView = (TextView) helloWorldActivity.findViewById(android.hello.R.id.tv);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testPreconditions() throws Exception {
        assertNotNull(textView);
    }

    public void testInputCodeField(){
        String actual=textView.getText().toString();
        String expected = "Hello, Android";
        assertEquals(expected,actual );
    }
}
Community
  • 1
  • 1
  • 1
    I've got this exact same issue – groodt May 13 '14 at 22:18
  • Set the theme before `super.onCreate();` When the super is called the default theme is loaded thus it will give you that Exception. – Ionut Negru May 27 '14 at 07:12
  • @IonutNegru This change does not affect the error. – Sabine Schneider Jun 03 '14 at 17:32
  • 10
    I'm seeing this in my unit tests too. I don't think this is a duplicate of the general question. I guess it's an issue with the MockContext. – murrayc Oct 28 '14 at 14:17
  • As murrayc said this is not a duplicate, currently this still happens to me, but only in my unit test with Robolectric. Seems a mock issue. – David Feb 26 '15 at 10:41
  • 2
    Using ActivityInstrumentationTestCase2 instead of ActivityUnitTestCase resolves this error in JUnit. But using ActivityInstrumentationTestCase2 for simple Unit Testing does not make sense to me. As of today(24th April 2015) there seems to be **NO** solution to test an activity which supports v7-appcompat ActionBar with ActivityUnitTestCase!! – Raghu Apr 23 '15 at 07:52
  • 1
    This question was not a duplicate. But @Raghu is correct. – EpicPandaForce Apr 30 '15 at 08:19
  • Facing same problem. BTW who marked this as duplicate as it is totally different problem! – Muhammad Babar May 19 '15 at 07:06
  • Ok guys i have found the solution thanks to `nhaarman` http://stackoverflow.com/questions/28843304/how-to-run-a-simple-junit4-test-in-android-studio-1-1 – Muhammad Babar May 19 '15 at 07:56
  • Unit test cases needs to be run as unit test (Build variant->Unit Test) and not as Activity Instrumentation. You need to create `src/test/java` structure for unit test. `androidTest` is for `Instrumentation Tests`. – Muhammad Babar May 19 '15 at 07:58
  • Not duplicated. Not even close... Still waiting for a solution to this problem – Corbella May 19 '15 at 11:23
  • I found a similar question not linked here which solved it for me. [This](http://stackoverflow.com/questions/22364433/activityunittestcase-and-startactivity-with-actionbaractivity) SO had a solution around ContextThemeWrapper that I used in my tests that were failing to use the right theme. – C Nick Jul 07 '15 at 19:06

2 Answers2

2

There are 2 things I'd try:

  • Remove the setTheme from onCreate, it is redundant with the manifest and may lead to confusion
  • Set the theme at Application instead of Activity level in the manifest
shalafi
  • 3,926
  • 2
  • 23
  • 27
  • 1
    Sadly, neither of your suggestions works for me (plus I didn't have setTheme in the onCreate at the time I asked this question, but have tried this out in the meantime as well). – Sabine Schneider Jun 03 '14 at 17:03
  • 4
    I have the same problem. My Activity class extends `AppCompatActivity` and when trying to test it with `ActivityUnitTestCase` with `@RunWith(AndroidJUnit4.class)` I see the error: `java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.` I tried to change the Manifest file to use `Theme.AppCompat` but it does not appear to have any influence on the tests. – Marcin Czenko Oct 20 '15 at 16:32
1

add android:theme="@style/Theme.AppCompat" under application in your manifest.xml

Howli
  • 12,291
  • 19
  • 47
  • 72
Toppers
  • 559
  • 3
  • 11