Thursday, May 23, 2013

Configuring Robolectric 2.0

In Robolectric 1.x, it was necessary to extend RobolectricTestRunner if you wanted to configure things differently.

From 2.0 on, most configuration can be accomplished using either the @Config annotation or by adding a special file on your classpath.

You can add the @Config annotation to individual test methods, or entire test classes.

If you'd like to apply a configuration to your entire project, create a file called "org.robolectric.Config.properties", and place it on your classpath. If you're using maven, you could put it in src/test/resources.

Example:

@Config(qualifiers="fr-land")
public void shouldDisplayThingsInFrench() {
  ...
}

Configurable values currently include which manifest to use, the SDK level and qualifiers to report, and the list of additional shadows to enable. See the @Config annotation documentation for details.

Also, remember that Robolectric will now look for a test version of your Application class, where you can do additional configuration which used to require subclassing RobolectricTestRunner.

34 comments:

  1. Thanks for providing this blog re configuration. I've been having trouble with the migration from 1.x to 2.x.

    Please provide a custom manifest configuration example. The documentation says "Robolectric will look relative to the current directory." What is the current directory from which I should specify the manifest file (how do I know where it is starting from)? Do I need to include the text "AndroidManifest.xml?"

    ReplyDelete
  2. The approach that ended up working for me was creating a configuration file at the path specified above with this content:

    manifest=./ProjectName/AndroidManifest.xml

    I'm guessing the standard configuration doesn't work for me because of my project structure:

    project/
    -ruby/...
    -android/ProjectName/
    - (standard Android project structure here)

    ReplyDelete
    Replies
    1. I am trying to use this approach and not sure if I need to create a file with exactly "org.robolectric.Config.properties" name and where to put it in my project. Right now I have it placed in my src directory where all my test cases reside. Also, do I need to add this file to my Android.mk ?

      Delete
  3. So, if I understood it correctly a shadow class should be annotated with

    @Config(shadows = ShadowYourClass.class)?

    ReplyDelete
  4. For my class say Log, I created a Shadow class "ShadowLog"

    @Implements(Log.class)
    public static class ShadowLog {
    public static int i(java.lang.String tag, java.lang.String msg) {
    System.err.println("[" + tag + "] " + msg);
    return 0;
    }
    }

    Now should I just say @Config(shadows = ShadowLog.class). How does Robolectric figures out which shadow class to call when I say

    Log.i("LogTest", "log message")

    ReplyDelete
  5. You can take a look at https://github.com/robolectric/robolectric/issues/594. It looks like shadowed objects are not instrumented so in your example method call will not be redirected to the shadow object

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Add the annotation @Config(manifest="../Android_Project_Under_Test/AndroidManifest.xml") in your Test class or create a file org.robolectric.Config.properties in test directory with following information
    manifest=../Android_Project_Under_Test/AndroidManifest.xml

    ReplyDelete
  8. I'm writing a custom shadow for GooglePlayServicesUtil, it contains static state that needs to be cleared up at the end of each test. I don't see a logical hook to provide this. I see that Robolectric.reset() is called from deep within ParallelUniverse but no natural way to provide automatic resetting of Shadows.... how about a @Reset annotation that if present on a shadow will automatically be called from here?

    ReplyDelete
  9. Im getting these warnings while unittesting using robolectric about missing AndroidManifest. I understand the configuration change i need to make here, but what if testing a library that doesn't have AndroidManifest.xml ?

    ReplyDelete
  10. Nice post! That's extraordinarily well explained and articulated notably favourable circumstances to hosting trade. currently am being an regular reader of your blogs. Thanks most for extraordinary assortment, keep writing.
    Thanks and cheers!

    DedicatedHosting4u.com

    ReplyDelete
  11. https://www.bedigitalpro.com/happy-new-year-2020/

    ReplyDelete
  12. Thanks for sharing good blog post. I really appreciate your post and you explain each and every point very well. Fashion bloggers in India

    ReplyDelete
  13. These cards are taking over New York
    smart carts
    whats your take??

    ReplyDelete
  14. What an amazing blog you have here Cocaine for Sale thank you for sharing this real good content buy colombian cocaine online will like to also say we have an amazing blog too if you will love to take a look buy peruvian cocaine online thanks for your time to check on our blog. Today cocainehydrochloride is one of the world-leading buy cocaine online manufacturers in the USA . for you to order cocaine online , there is a variety of cocaine websites you can purchase and have it delivered Worldwide . And Yes, you can buy crack cocaine online illegal drugs on the Internet, and it's a lot safer

    ReplyDelete
  15. What a good blog you have here on how to buy Vyvanse online medication. There are many order Vyvanse online
    pharmacy websites that operate legally such as buy adderall XR online and offer convenience, privacy, and safeguards for purchasing buy oxycodone online medications. Also, these medicines may not have been stored properly . So always look out Buy Keifeitropin 140iu Online for details

    ReplyDelete
  16. Xanax belongs to the benzodiazepines drug, which is using to address anxiety, panic disorder, and stress by stimulating the disturbed and unbalanced chemicals in the brain. Xanax offers calming effects in the brain to enhance the productivity evaluator's consultation and guidelines. Buy Xanax online

    buy xanax online

    ReplyDelete
  17. Oxycontin may be a brand of Oxycodone, this is often the controlled-release Oxycodone tablets, intended to be taken every 12 hours. Oxycodone may be a semi-synthetic opioid synthesized from thebaine, an opioid alkaloid found in the Persian poppy, and one among the various alkaloids found within the Papaver somniferous. buy oxycontin online

    buy oxycodone online

    ReplyDelete
  18. Adderall Online is being used under the observation of health experts to address ADHD and narcolepsy without causing any future health issues. Therefore, it works to promote a healthy and progressive mindset by reducing the impact of aggression, stress, and anger. and the formula of Adderall is C9H13N.buy Adderall online

    buy adderall online

    ReplyDelete
  19. Oxycontin may be a brand of Oxycodone, this is often the controlled-release Oxycodone tablets, intended to be taken every 12 hours. Oxycodone may be a semi-synthetic opioid synthesized from thebaine, an opioid alkaloid found in the Persian poppy, and one among the various alkaloids found within the Papaver somniferous. buy oxycontin online

    buy oxycontin online

    ReplyDelete
  20. Norco may be a powerful prescription pain drug that comprises an opioid (narcotic) that’s utilized to regulate pain severe sufficient to wish an opioid analgesic, while other pain therapies like non-opioid ache medicines don’t manage your illness well adequately, otherwise you cannot bear them. Buy Norco online

    buy norco online

    ReplyDelete
  21. This drug helps in relieving moderate to severe pain. Vicodin contains a narcotic analgesic (hydrocodone) and a non-opioid pain reliever (acetaminophen). Hydrocodone helps the brain to change how your body feels and responds to pain. Buy Vicodin online

    buy vicodin online

    ReplyDelete
  22. This drug helps in relieving moderate to severe pain. Vicodin contains a narcotic analgesic (hydrocodone) and a non-opioid pain reliever (acetaminophen). Hydrocodone helps the brain to change how your body feels and responds to pain. Buy Vicodin online

    buy vicodin online

    ReplyDelete
  23. Its always nice and i feel honored reading your Blog . Your Blogs always based on all basic concepts which anyone can understand so easily and relate to Pleasure to read your Blogs Sir.



    golden retriever puppies for sale near me
    dachshund puppies for sale near me
    golden retriever puppies for sale in pa

    ReplyDelete