Friday, April 5, 2013

The Test Lifecycle in 2.0

Starting with Robolectric 2.0 alpha 3, there are some changes in how Robolectric prepares to run your tests.

Finding Your Application

By default, Robolectric first looks in your AndroidManifest.xml for the class to load as your application:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp">
  <application android:name=".Application"/>
</manifest>

That will cause a class called com.myapp.Application to be loaded. However, if you create a class with the same name and package, but with "Test" prepended to the class name, Robolectric will load that instead.

package com.myapp;
class TestApplication extends Application {
}

The Application Lifecycle

Robolectric now automatically calls an application's onCreate() method before the test runs, and onTerminate() after the test runs. Note that this a change from earlier versions. If you want to prevent your application's onCreate() method from being called, you can override it in a test class and have it do nothing.

If your test application implements the TestLifecycleApplication interface, it will get called with a few more useful events:

class TestApplication extends Application
    implements TestLifecycleApplication {
  @Override public void beforeTest(Method method) {
  }

  @Override public void prepareTest(Object test) {
  }

  @Override public void afterTest(Method method) {
  }
}

The overall steps taken when running each test, then, are:
  1. Create your application.
  2. Call application.onCreate().
  3. Call application.beforeTest().
  4. Call application.prepareTest().
  5. Run the test.
  6. Call application.onTerminate().
  7. Call application.afterTest().

These changes should make it possible for many people to avoid overriding RobolectricTestRunner altogether. Note that you can still change this by using your own subclass of RobolectricTestRunner, which provides a different TestLifecycle class.

85 comments:

  1. Sweet!

    But any plans to allow simpler tests to avoid dealing with application bootstrap altogether? If all you need is to mock a few of Android's final classes that's a lot of overhead.

    ReplyDelete
    Replies
    1. Yep, good point. We could make it switchable, something like @Config(createApplication = false).

      Regarding test startup time, the biggest cost by far now is reading in all the resource files. I think we'll get a huge win by lazily loading stuff where we can, which is close on the list.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Any progress on either issue here?
      MikiNW

      Delete
  2. A few questions:

    1. Where to find Robolectric 2.0 alpha 3? It doesn't appear to be on maven.
    2. We're working on a relatively new project, is 2.0 alpha 3 stable enough for us to use, or will we constantly be running into weird issues that will be resolved prior to release?
    3. Is 2.0 compatible with junit 4.11? I assume it is but thought I'd ask (while I'm at it).

    ReplyDelete
  3. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete
  4. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete

  5. "That’s a huge collection of your blog. Really it’s amazing"!!
    "this is superb.. thank you.."!!
    iphone app developer

    ReplyDelete
  6. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Mobile App Development Company
    Mobile App Development Companies

    ReplyDelete
  7. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Android App Development Company

    ReplyDelete
  8. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Mobile App Development Company

    ReplyDelete
  9. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    Web Design Development Company
    Web design Company in Chennai
    Web development Company in Chennai

    ReplyDelete
  10. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Fitness SMS
    Salon SMS
    Investor Relation SMS

    ReplyDelete
  11. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  12. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    iOS App Development Company

    ReplyDelete
  13. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  14. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    snapho

    ReplyDelete
  15. Hi,

    Thanks for sharing a very interesting article about The Test Lifecycle in 2.0. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

    From,
    Maestro Infotech,
    Web Design Company Bangalore

    ReplyDelete
  16. thank you for sharing an excellent post.

    https://www.creatorswebindia.com/

    Web Design in chennai

    Web design Company in chennai

    Website Design in chennai

    Website Design Company in chennai

    website company in chennai

    Web development in chennai

    Web development Company in chennai

    Web Designer in chennai

    Web Designer Company in chennai

    website designer in chennai

    website designer company in chennai

    ReplyDelete
  17. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us. Also, don't forget to read the latest taco bell customer satisfaction survey here.tellthebell
    Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us. Also, don't forget to read the latest taco bell customer satisfaction survey here.tellthebell

    ReplyDelete
  18. Hey Blogger, you have written a masterpiece with lots of information and I must say that you should post daily. Visit Us too kroger feedback

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

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

    ReplyDelete
  21. http://pixselo.com/our-services/web-development-company/

    Pixselo Global Solutions - Digital | Technology | Media

    ReplyDelete
  22. Thanks for sharing informative article. Asperina Technosoft is leading web development company offering professional service to customers across the globe at affordable price. Follow us at Facebook.

    ReplyDelete
  23. First this is a really Creative and Unique Article. Well, the main reason of me sharing my post here is that, We being the Digital Marketing Company In India are open for Partnerships with Mobile App Development, Website Development and Graphics related company !

    Get in touch with the Digital Marketing Services In India for more information

    ReplyDelete
  24. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference.

    ReplyDelete
  25. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference.

    ReplyDelete
  26. Excellent post. I learned a lot from this blog and I suggest my friends to visit your blog to learn new concept about technology.
    Data Science Course in Chennai
    Data Science Training in Chennai
    Data Analytics Courses in Chennai
    Big Data Analytics Courses in Chennai

    ReplyDelete
  27. First this is a really Creative and Unique Article. Well, the main reason of me sharing my post here is that, We have written a detailed post on the which can be used by Content Marketing Companies. Do check it out and let us know.

    Top Digital Marketing Companies in India

    List of Best Digital Marketing Companies in Bangalore

    Content Writing Samples

    ReplyDelete
  28. Excellent Post. it is very useful and informative post. Thank you for your sharing.
    Ecommerce Web Development Company In Chennai | Digital Marketing Services In Chennai

    ReplyDelete
  29. Looking to add extra style to your content, make use of our strikethrough Text Generator to add amazing line through text in desired platform. Strikethrough in Google Docs.

    ReplyDelete
  30. Very informative article..Thanks for sharing the best info for android Application...

    ReplyDelete
  31. EPSXE APK:- EPSXE is a Playstation emulator which is available for Android users. This EPSXE was a very popular port for pc. EPSXE has many superlative


    ePSXe for Android

    ReplyDelete
  32. https://www.veilleuse.shop/produit/veilleuse-coranique-munawara/
    La veilleuse coranique bluetooth avec sa télécommande pour offrir.
    Cadeau ramadam idéal
    La veilleuse coranique personnalisée pas cher
    Veilleuse coranique personnalisée
    Veilleuse coranique personnalisée

    Découvrez La veilleuse coranique Munawara
    Video de la Veilleuse coranique munawara
    Veilleuse coranique munawara


    Je travailles sur un projet de fabrication de cornes de gazelle personnalisée
    cornes de gazelle expressives
    cornes de gazelle délicieuses
    Merci de laisser ce lien c'est sympa...

    Le casque vapeur hair steamer permet de lutter contre la sécheresse, la chute des cheveux et leur mauvaise santé , dans le confort de votre domicile. Le hair steamer est un casque vapeur qui apporte une dose d'hydratation pour les cheveux crépus.
    Hair steamer vapohair
    Lee hair steamer casque vapeur est recommendé par fes femmes aux cheveux crépus
    Casque vapeur
    La casque vapeur hair steamer apporte beaucoup de bienfait au cheveux crépus de type afro Hair.
    hair steamer Casque vapeur hydratation cheveux crépus
    hair steamer
    Le hair steamer casque vapeur fournit une cure intense contre les chutes et pour favoriser la repousse.
    Le hair steamer est un casque à vapeur sûr, une utilisation et un entretien facile

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

    ReplyDelete
  34. Hi! Thanks for sharing this very interesting article Check out our latest TacobellSurvey.

    ReplyDelete
  35. The post is really good, we will use above mentioned strategy in our SEO services in Bangalore.

    ReplyDelete
  36. You can write a song about anything, but that sometimes makes it harder to get started than anything else. Some people use experiences from their personal life as inspiration, and others write things that they have read about. No matter what you choose to write on, anyone can write their own easy way to write a song lyrics with a bit of practice.

    ReplyDelete
  37. This is really a very good blog post and thanks for sharing it with the community!
    professional web design company in chennai
    web design company in chennai

    ReplyDelete