问题描述
我尝试测试活动,它使用ActionBarActivity(从appcompat库)。我需要一个自定义的应用程序能够操纵DI系统加载后台服务我的测试服务,而不是。
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.
如果我有我的测试编写,并调用startActivity我收到以下错误:
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.
如果我打电话launchActivityWithIntent活动启动时没有任何问题,但它是用嘲笑的应用程序类的我真正应用类代替。任何想法如何,我可以解决这个问题或者我怎么能执行code中的应用程序的onCreate被称为后,但之前的onCreate我的活动获取的所谓的在我的仪器测试?
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?
推荐答案
我发现,如果我创建一个自定义MockApplication并添加以下code:
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.
这篇关于ActivityUnitTestCase和startActivity与ActionBarActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!