What is Unit Test

Unit test is a process where each individual unit of software are tested.The purpose is to test a method or function so that execute as expected.The tests are performed on JVM to reduce execution time.Sometime unit test are written before code.

What is use of writing unit test?

To ensure that developers are writing high quality and error less code.

Unit test save times during development to test weather our code execute in same way as it should be executed.

As it helps to find the bugs in the early stage of development and reduces the development cost and time

Types of unit test?

Local Test :which run on local Java Virtual Machine that reduce the execution time.

Instrumented test: unit test that runs on android device or emulator

why we have to use Roboelectric?

If you need to a write test that contain object from android framework then i recommend to use Roboelectric

Roboelectric framework make unit test fast and reliable so lets have a look in to it in depth

Roboelectric framework helps to write unit test and runs on desktop JVM while using android api.By using this framework we can run our android test on the continuous intergration without any additional feature.

It support resource handling it inflate views,by using findviewbyId() we can find specific view.

if a class contain Roboelectric code for testing the we have to provide an annotation with @RunWith(RobolectricTestRunner.class)

@Before is called before the test going to perform it act as intalizing part for actvity or other.

@Test make our method testable with certain operation

Lets Quickly Jump in to an example to see how it work

In the setupp() metod we are intialzing the activity before running the test which is denoted with annotaion @Before

And in checknotnull() Method we are testing weather the actvity is null or not

And in movetonextactvity() method we are checking weather the activity moving from screen one to screen two as we expected

Shadow Classes that represent the core library of android.

Advantage of Using Roboelectric

1.Robolectric does not require emulator to run its test cases, makes it fast and easy to execute multiple test cases at one time.

2.Robolectric can fake almost every android related thing as Shadow Objects, each shadow object exposes many useful functions which their normal android counterparts don’t offer.


Robolectric does not cover all the functionality a real device or emulator can offer like Sensors, OPENGL etc.

2. For doing integration or UI testing emulator or real device is required.

Knowledge is everything.Check out my app at https://play.google.com/store/apps/details?id=com.vibe.britevibe

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Kotlin Features You’ve Been Missing Out On

ML with Android

Biometric Authentication in Flutter

Announcing the release of Flutter 2-What is new in it?

Android Scoped Storage Demystified

WorkManager (Android Architecture Component), Background Task + Kotlin

How to unit test your Flutter loading widgets

Latest Method: How to get Current Location (Latitude and Longitude) in Android & Give support for…

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
Pandiyan Mani

Pandiyan Mani

Knowledge is everything.Check out my app at https://play.google.com/store/apps/details?id=com.vibe.britevibe

More from Medium

Dependency Injection Simplified

Gitlab CI automation : Nice things to know before starting!

What I Learned at Work this Week: Accept-Language HTTP Header

How to Create Dynamic Libraries in C