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

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


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


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.


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 {@Testpublic 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!"));}}`