Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 856
fkrauthan Avatar asked Mar 12 '14 21:03

fkrauthan


1 Answers

The accepted answer didn't work in my case, but including something this in the ActicityUnitTestCase subclass worked for me:

@Override
public void setUp(){
    ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
    setActivityContext(context);
}
like image 179
akiraspeirs Avatar answered Oct 05 '22 09:10

akiraspeirs