Pandiyan Mani
2 min readAug 17, 2021

--

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.

@RunWith(RobolectricTestRunner.class)

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

@RunWith(RobolectricTestRunner.class)
public class MainclassTest
{
private Mainclass mainclass;

@Before
public void setupp() throws Exception
{
mainclass = Robolectric.buildActivity(Mainclass.class).create().resume().get();
}

@Test
public void checknotnull() throws Exception
{
assertNotNull(mainclass);
}

@Test
public void movetonextactvity() throws Exception
{
Button button_start =mainclass.findViewById(R.id.button_start);
button_start.performClick();
ShadowActivity sha = shadowOf(mainclass);
Intent in = sha.getNextStartedActivity();
ShadowIntent ins = shadowOf(in);
assertThat(in).isSameAs(Mainclass2.class.getName());
}

}

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.

Disadvantage

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.

--

--