43

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35

3 Answers3

49

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

Jahnold
  • 7,623
  • 2
  • 37
  • 31
  • 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? – Deepanker Chaudhary Jun 09 '16 at 09:48
  • Well it might not be as clear cut but yes that is the general idea. – Jahnold Jun 09 '16 at 10:30
  • Take a look at the tests for this example app which might give you some ideas: https://github.com/emmaguy/rxjava-mvp-giphy – Jahnold Jun 09 '16 at 12:11
  • 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. – Robert R Evans Jun 09 '16 at 22:28
  • 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 ? – Doug Ray Aug 17 '16 at 18:48
  • @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. – Jahnold Aug 22 '16 at 22:31
  • @Jahnold thanks I will try some tests out running on Roboeletric test runner. – Doug Ray Aug 23 '16 at 23:16
  • @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 – Mr.G Jul 14 '17 at 07:08
15

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.

Eric Liu
  • 1,516
  • 13
  • 19
  • 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 Jun 09 '16 at 08:25
  • 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. – Talha Mar 09 '18 at 11:41
-1

First of all we need to understand that Roboelectric and Mockito are two different tools commonly used in android Test Driven Development. So mostly you will find both of the tools in a same project.

Below I am explaining the common use cases for both-

Mockito is used for mocking the dependency which means if you want to access an real object in test environment then you need to fake it or we can say mock it. Now a days it is very easier to do mocking of the objects with Mockito.

Roboelectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a JVM, without the overhead of an emulator. Simple test written using roboelectric is

`@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Test
public void clickingButton_shouldChangeResultsViewText() throws Exception {
Activity activity = Robolectric.setupActivity(MyActivity.class);

Button button = (Button) activity.findViewById(R.id.press_me_button);
TextView results = (TextView) activity.findViewById(R.id.results_text_view);

button.performClick();
assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
}
}`
Shivam Yadav
  • 958
  • 11
  • 23
  • 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? – gmatcat Jan 05 '21 at 14:43