Unit Test Cases with JUnit +(Robolectric or Mockito or both in Android)

10,631

Solution 1

They have slightly different usages and I tend to use both in my projects.

Mockito

is used for making mocks of your classes.

When you are testing a particular class you mock all of its dependencies with Mockito.

Where possible most of your tests should use mockito. To make this possible most people split their code up into MVP, etc where the business logic is separated from the View logic. This way your business logic (Presenter) has no knowledge (or dependencies) on the Android library and has no need to have mocks of them.

Robolectric

is a library which contains many mocks of Android classes.

The Robolectric test runner injects these 'shadow objects' in place of the actual Android classes when the tests are run. This is what allows the tests to run on the JVM without booting up an instance of Android.

When using MVP your View layer tends to be implemented by the Activity/Fragment and this is where you can use Robolectric to mock these.

Notes

Use Robolectric only where necessary. It basically re-implements parts of the Android framework but not always in exactly the same way.

You may also need another library such as PowerMock. This allows the mocking of static classes such as Math or can be used to mock static Android classes such as TextUtils.

Both are used with JUnit

Solution 2

Mockito alone can cover most cases.

However, Robolectric can also provide limited operations on Android Component such as Activity or Fragment in Unit Test (not instrumentation test, which has no dependency on Android SDK), which does not require any emulator or devices and is considerably faster than instrumentation tests.

My suggestion: use Mockito for unit test and Espresso for UI test since they are semi-official test frameworks for Android.

Add Robolectric in your Unit Test if there are some dependencies on Android SDK.

Share:
10,631
Deepanker Chaudhary
Author by

Deepanker Chaudhary

I have 9 Years 6 Months of experience as an Android Developer. I have following skills: Android Kotlin Java Flutter Angular

Updated on June 06, 2022

Comments

  • Deepanker Chaudhary
    Deepanker Chaudhary about 2 years

    This is first time I have to write unit test cases in Android.

    So I have searched lots of things.

    1. Robolectric framewordk - Runs on JVM
    2. Mockito Framwork - Mocking objects

    So I have some doubts in Robolectric & Mokito.

    1. Should I have to use Robolectric only with JUnit in Android app?
    2. Should I have to use Mockito only with JUnit in Android app?
    3. Should I have to go with both framework?
    4. What is the difference between Mockito & Robolectric?

    I have search for difference between Mokito & Robolectric but don't get any proper answer for that.

    Please suggest.

  • Deepanker Chaudhary
    Deepanker Chaudhary about 8 years
    I have activity & fragments & as well as web services. i have to do only unit testing not instrumental. So I understand robolectric but i don't too much idea that where i will use robolectric or where mockito. or mockito only will be use for model classes?
  • Deepanker Chaudhary
    Deepanker Chaudhary about 8 years
    I have Activity & Fragment so for that i will use robolectric & for my model classes which is related to web services i will use mockito. Right?
  • Jahnold
    Jahnold about 8 years
    Well it might not be as clear cut but yes that is the general idea.
  • Jahnold
    Jahnold about 8 years
    Take a look at the tests for this example app which might give you some ideas: github.com/emmaguy/rxjava-mvp-giphy
  • Robert R Evans
    Robert R Evans about 8 years
    As Jahnold says, Robolectric is a library that replaces Android-specific libraries. You do this in order to do unit testing on your PC or Linux platform, using the Java VM. If you execute your unit tests on an emulator or on a USB-connected Android device, you do not use Robolectric. Mockito is a framework to replace modules in your system that you do not want to use during a unit test of another module - we say you "mock" the dependent module.
  • Doug Ray
    Doug Ray almost 8 years
    Hey thanks for the info I am confused about running a test with Junit + Roboectric. Mostly beacuse of this line @RunWith(AndroidJUnit4.class) at the top of the test class. So should it be run with Junit or run with robo ?
  • Jahnold
    Jahnold almost 8 years
    @DougRay it should be run with Robolectric. Robolectric is a JUnit 'Test Runner' which basically means it sits on top of JUnit. If you need more guidance then it might be worth submitting your own question.
  • Doug Ray
    Doug Ray almost 8 years
    @Jahnold thanks I will try some tests out running on Roboeletric test runner.
  • Mr.G
    Mr.G almost 7 years
    @Jahnold i need to run test case to execute service layer in my android app .So what framework is ideal ? Robolectric . or Mockito , and later this will be an whole app . i herad for the Ui testing Espresso is ideal
  • Talha
    Talha over 6 years
    Adding to that and to your comment, If your business logic layer is purely independent of Android Framework, you should use JUnit with Mockito, only if your business logic layer has Android Framework dependencies then you need to use Roboelectric.
  • gmatcat
    gmatcat over 3 years
    I just wanted to ask why dagger in necessary in unit testing? Injecting other functioning dependencies into a unit is no longer a "unit" test. If other dependencies are required in a test shouldn't it be called an integration test? Pretty new to unit testing and don't really know which is which. If the purpose of dagger is to inject a "dummy" dependency into a unit test, isn't it easier to just mock it?