Robolectric

In AndroidPub. More on Medium.

Robolectric is a great tool to test classes that depend on Android views and classes properly. I use Robolectric heavily to test custom view logic.

I wrote a custom view to mimic TextInputLayout with some added app specific features. I also did the hint to label and label to hint animation in the custom view to mimic this.

Take from https://material.io/design/components/text-fields.html#anatomy

So I have a showLabel() method which animates hint to label text and hideLabel() method which animates label to text.

So our naive test class for this view would look like this.

As mentioned in the comment, the assertions…


We can all relate to how annoying it might be to hit the “run” button in Android Studio, only to find yourself having to wait for a while for the emulator to generate, load, and build your project. Thankfully, testing libraries and frameworks have been created to prevent us from doing this all the time. Continuing from my previous guide where we looked at the basics of Android testing, we’ll now be looking at how to set up Robolectric in your project and use it to write unit tests!

Setting it up

The very first thing you’ll need to do, as with all…


Source: Google

Why do we need?

Have you ever felt boring when you have to run your app for a small little change to see it is working fine or not?

If yes then, you must try Robolectric, it is a unit testing framework that allows Android applications to be tested on the JVM without an emulator or device.

These are the main features of it:

  1. It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator.
  2. Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric. It is a replica of Android class with…

AndroidPub

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store