Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to test with Robolectric?

It seems to me that I am fundamentally misunderstanding the purpose of Robolectric. I've been battling with it for a week already, and so far getting a new error message is considered as making progress. I am able to test some basic stuff like static views in an activity, but when something more complicated things come into play things just fall apart. I had to extend Robolectric to support 3-rd party libraries with certain parameters, Appcompat action bars and numerous other things which was extremely time-consuming and wasn't really documented anywhere, and things are advancing at a pretty much glacial pace. I am starting to think that I am using it in a wrong way and it simply isn't supposed to do what I want it to do.

The general app logic is quite straightforward so there isn't really much to unit test, the most complicated stuff is in the UI and remote API calls. Is Robolectric just supposed to make unit testing for Android less painful than with JUnit because it can run on the JVM and supports a few Android classes? Perhaps a black-box behaviour testing framework like Espresso would be more suitable for my needs? But we use continuos integration, and Robolectric was nice and easy to set up to run tests on the CI server, and I'd kind of like to keep it that way.

What do you use Robolectric for? A lot of blog posts recommend it for "activity lifecycle testing", but since I'm also quite new in the Android world, I don't really understand the purpose of it, especially since the app I'm testing is portrait-only. Could someone please give an overview of what you use Robolectric for, and how do you do it, preferably with code examples and explain why and how those tests are important?

like image 748
p4sh4 Avatar asked Oct 20 '22 21:10

p4sh4


1 Answers

We use it for:

  • unit testing: all components from parsers and utils, to controllers and presenters
  • integration/acceptance testing: the business logic of the app, per screen (which falls into integration and/or acceptance testing)

We don't use it for (and have found it difficult to use for these):

  • testing the network layer (we run all tests by injecting the test data in the same way the network layer would; parsers are tested separately)
  • user flows through different screens

If you're looking for more of the latter, perhaps Espresso/Robotium are better suited for your needs. And you absolutely can run these as part of your CI pipeline, but you'll need to invest some time in setup, or integrating with something like Appurify.

If you are finding it very difficult to write your tests, it might have to do more with the way your app is architected than the way you're using robolectric. See my answer here as well, it might help you: Writing Android acceptance tests with robolectric: how could it be done?

like image 159
Alex Florescu Avatar answered Oct 22 '22 10:10

Alex Florescu