weixin_39538607
weixin_39538607
2021-01-12 13:52

How to make robolectric work with ActionBarActivity

When i using robolectric testing ActionBarActivity from support v7 package, it always gives the following error.

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:102) at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58) at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) at me.biubiubiu.rms.BaseActivity.onCreate(BaseActivity.java:59) at me.biubiubiu.rms.ItemBaseActivity.onCreate(ItemBaseActivity.java:54) at android.app.Activity.performCreate(Activity.java:5008) at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) at org.robolectric.util.ActivityController$1.run(ActivityController.java:116) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) at org.robolectric.util.ActivityController.create(ActivityController.java:111) at org.robolectric.util.ActivityController.create(ActivityController.java:123) at me.biubiubiu.rms.ModelTest.clickingButton_shouldChangeResultsViewText(ModelTest.java:79)

And here is the code i use to launch this activity.

public void clickingButton_shouldChangeResultsViewText() throws Exception { Activity activity = Robolectric.buildActivity(MyActivity.class).create().get(); }

该提问来源于开源项目:robolectric/robolectric

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

30条回答

  • weixin_39622628 weixin_39622628 4月前

    hi, please look at my app which is being tested in Android 1.3

    https://github.com/juanmendez/jm_android_dev/tree/master/10.services/02.intentService

    make sure you have this file on your workspace, next to manifest. https://github.com/juanmendez/jm_android_dev/blob/master/10.services/02.intentService/app/src/main/project.properties

    declare accordingly your robolectric dependency. https://github.com/juanmendez/jm_android_dev/blob/master/10.services/02.intentService/app/build.gradle

    see my annotations on top of this class. https://github.com/juanmendez/jm_android_dev/blob/master/10.services/02.intentService/app/src/test/java/ContentProviderTest.java

    I hope this helps.

    点赞 评论 复制链接分享
  • weixin_39725154 weixin_39725154 4月前

    have you tried upgrading to robolectric-3.0? You still use 3.0-rc2 in your example. I can't make it working with 3.0.

    点赞 评论 复制链接分享
  • weixin_39622628 weixin_39622628 4月前

    Is 3.0 an upgrade? or a downgrade? anyways I tried it and it also worked.

    点赞 评论 复制链接分享
  • weixin_39924779 weixin_39924779 4月前

    I'm using 2.3 (and have tried 2.4-SNAPSHOT) but I'm still running into this issue when trying to run tests. I don't get this error at all when running or debugging my application normally.

    I'm currently using the workaround detailed in #1150 in order to get resources to load correctly when using applicationIdSuffix. Could this be related?

    点赞 评论 复制链接分享
  • weixin_39725154 weixin_39725154 4月前

    Still present with 2.4. I've started a fresh project in IDEA with Android support, generated an Activity with Fragment (which happens to extend ActionBarActivity), and experience the same error when running a test that creates the Activity. Of course the app itself runs fine. I suggest reopening the issue. It's also mentioned (and unanswered) in the forums.

    点赞 评论 复制链接分享
  • weixin_39793098 weixin_39793098 4月前

    This is working in 3.0-rc2. See the android-appcompat-v7 sample in the robolectric/robolectric-samples repository.

    点赞 评论 复制链接分享
  • weixin_39725154 weixin_39725154 4月前

    Confirmed, works fine with 3.0-rc2.

    点赞 评论 复制链接分享
  • weixin_39960700 weixin_39960700 4月前

    No, it's still present. I have this issue with 3.0-rc2 with AppCompatActivity from android-appcompat-v7 version 22.2.0.

    点赞 评论 复制链接分享
  • weixin_39660922 weixin_39660922 4月前

    I got the same problem with 3.0-rc2. Not fixed.

    点赞 评论 复制链接分享
  • weixin_39675728 weixin_39675728 4月前

    I don't have a problem with the activity itself, but using the android.support.v7.app.AlertDialog causes the error (3.0-rc3).

    
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatDialog.__constructor__(AppCompatDialog.java:48)
        at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java)
        at android.support.v7.app.AlertDialog.<init>(AlertDialog.java)
        at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:882)
    </init></init>
    点赞 评论 复制链接分享
  • weixin_39932947 weixin_39932947 4月前

    Still happening for AppCompat lib. Using version 2.4 of Robolectric.

     java
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
        at com.oneupsports.coreapp.ui.MainActivity.onCreate(MainActivity.java:47)
        at android.app.Activity.performCreate(Activity.java:5008)
        at org.robolectric.internal.ReflectionHelpers$3.run(ReflectionHelpers.java:64)
        at org.robolectric.internal.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:114)
        at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:59)
        at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
        at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
        at org.robolectric.util.ActivityController.create(ActivityController.java:111)
        at org.robolectric.util.ActivityController.create(ActivityController.java:122)
        at org.robolectric.util.ActivityController.setup(ActivityController.java:202)
        at org.robolectric.Robolectric.setupActivity(Robolectric.java:1388)
        at com.oneupsports.coreapp.XmlConfigurationParserTest.setup(XmlConfigurationParserTest.java:24)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    
    点赞 评论 复制链接分享
  • weixin_39622628 weixin_39622628 4月前

    Using AndroidStudio 1.3.preview. I included the AndroidManifest.xml

    (RobolectricTestRunner.class) (manifest="app/src/main/AndroidManifest.xml") public class TestMainActivity

    Then I got a runtimeexception saying it couldn't find any resource from reference ResName.

    So What i did next was to go to the path of my project app/src/main, so created a new file (project.properties) which is at the same level as the manifest file.

    There I added in the new file the following. android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.0

    (answer came from https://github.com/robolectric/robolectric/issues/1334) and it worked!

    点赞 评论 复制链接分享
  • weixin_39604350 weixin_39604350 4月前

    The problem still exists with AppCompatActivity and ActionBarActivity. Robolectric 3.0 and target SDK 22.

    点赞 评论 复制链接分享
  • weixin_39622628 weixin_39622628 4月前

    I don't see that problem using

    dependencies { ......... testCompile "org.robolectric:robolectric:3.0-rc2" testCompile "junit:junit:4.12"; }

    点赞 评论 复制链接分享
  • weixin_39573136 weixin_39573136 4月前

    It works with 's method

    点赞 评论 复制链接分享
  • weixin_39622628 weixin_39622628 4月前

    I'm glad it worked for you !

    点赞 评论 复制链接分享
  • weixin_39604350 weixin_39604350 4月前

    Unfortunately still not working, after double checking the dependencies and adding (manifest="app/src/main/AndroidManifest.xml") to the Activity under test.

    点赞 评论 复制链接分享
  • weixin_39644139 weixin_39644139 4月前

    Have you tried with 2.3-SNAPSHOT?

    点赞 评论 复制链接分享
  • weixin_39538607 weixin_39538607 4月前

    I have tried, 2.3-SNAPSHOT, but still got the same error. I really hope to fix this issue, because robolectric help me a lot in TDD.

    点赞 评论 复制链接分享
  • weixin_39632471 weixin_39632471 4月前

    I'm seeing the same thing. Funny thing is that I've managed to get it working within my IDE, but it doesn't work in our continuous build system, which is a proprietary build system.

    Seems like the style is not being correctly applied:-

    
    ActivityController<testbasefragmentactivity> testBaseFragmentActivityActivityController = Robolectric
        .buildActivity(TestBaseFragmentActivity.class);
    activity = testBaseFragmentActivityActivityController.attach().get();
    TypedArray a = activity.obtainStyledAttributes(R.styleable.ActionBarWindow);
    boolean hasActionBar = a.getBoolean(R.styleable.ActionBarWindow_windowActionBar, false);
    </testbasefragmentactivity>

    hasActionBar == true within the IDE hasActionBar == false within the continuous build system

    I could do with some pointers to track this down. I know I need to provide the resources for both my own app and the library project on the file system, and I know Robolectric does some mapping between the values in the xml files and the compiled R.* resources, I've tried to ensure that both the compiled resources from my projects application package using aapt (which includes the library resources) and the compiled resources from the appcompat project in its own package namespace is there, but I must be missing something.

    Any pointers would be appreciated :-)

    点赞 评论 复制链接分享
  • weixin_39881760 weixin_39881760 4月前

    Any news on that ticket? I run unto the same issue

    点赞 评论 复制链接分享
  • weixin_39605345 weixin_39605345 4月前

    Same issue....

    点赞 评论 复制链接分享
  • weixin_39980929 weixin_39980929 4月前

    Same here:

    java.lang.NullPointerException at java.util.regex.Matcher.getTextLength(Matcher.java:1140) at java.util.regex.Matcher.reset(Matcher.java:291)

    with (reportSdk=10).

    点赞 评论 复制链接分享
  • weixin_39904522 weixin_39904522 4月前

    same issue here...

    点赞 评论 复制链接分享
  • weixin_39973196 weixin_39973196 4月前

    I use robolectric-2.3-20140402.155229-152-jar-with-dependencies.jar and according to this post http://robolectric.org/eclipse-quick-start/ it seems work fine w/ ActionBarActivity.

    点赞 评论 复制链接分享
  • weixin_39644494 weixin_39644494 4月前

    but in the Android Studio?

    点赞 评论 复制链接分享
  • weixin_39973196 weixin_39973196 4月前

    no lucky in Android Studio 0.53

    点赞 评论 复制链接分享
  • weixin_39612110 weixin_39612110 4月前

    This should be resolved with the current 2.3-SNAPSHOT build. If your CI build is failing, make sure you have setup project.properties according to http://robolectric.org/apklibs/. Feel free to reopen this issue if I'm totally wrong and it's not working still.

    点赞 评论 复制链接分享
  • weixin_39589557 weixin_39589557 4月前

    In analyzing the latest 2.3-SNAPSHOT jar, I see no "android/support/v7" directory. Deriving an activity from ActionBarActivity results in a class-not-found-exception for "android/support/v7app/ActionBarActivityDelegate". Is there an additional support jar that I need or how can I get past this?

    点赞 评论 复制链接分享
  • weixin_39793098 weixin_39793098 4月前

    android-support-v7 is not bundled with Robolectric. You need to pull it from the SDK and included it in your build.

    点赞 评论 复制链接分享