Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 911
Deepanker Chaudhary Avatar asked Jun 09 '16 08:06

Deepanker Chaudhary


People also ask

What is the difference between Mockito and Robolectric?

This is in contrast to using pure Mockito, which often ends up with tests that are the reverse of the logic you are attempting to test. Robolectric works by emulating the native calls the SDK would make on Dalvik, but on the JVM, so it can run much, much faster. This makes full unit testing and TDD possible.

Is JUnit and Mockito are same?

JUnit is a framework that helps with writing and running your unit tests. Mockito (or any other mocking tool) is a framework that you specifically use to efficiently write certain kind of tests.

How can write unit test cases for activity in Android?

unit tests can be written under test java package, and instrumental tests can be written under androidTest package. You can then access UI widgets using Espresso ViewMatchers and then you can apply actions on them using ViewActions . Check documentation for further help. Show activity on this post.


1 Answers

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

like image 66
Jahnold Avatar answered Oct 08 '22 01:10

Jahnold