Search code examples
androidunit-testingandroid-activityandroid-actionbarandroid-actionbar-compat

ActivityUnitTestCase and startActivity with ActionBarActivity


I try to test a Activity which uses ActionBarActivity (from the appcompat library). I need a custom Application to be able to manipulate the DI system to load my test service instead of the real service.

If I have my test written and call startActivity I get the following error:

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

If I call launchActivityWithIntent the Activity starts without any problems but It is using my Real Application class instead of the Mocked Application class. Any ideas how I can fix that or how I can execute code after onCreate of the application was called but before onCreate of my Activity get's called within my instrument test?


Solution

  • I found out that if I create a custom MockApplication and add the following code:

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

    I hope that will work for other people as well.