Writing your first local unit test in Android

Hello everyone. I hope you are doing well. This blog post is about writing your first local unit test in Android. Testing is an integral part of the Software Engineering and testing will help you write software that is robust and behaves the way you intend it to. Tests can also serve as future reference documents as to how the software was originally built to work.

Types of testing in Android

Local Unit Test vs. Instrumented Tests

Instrumented tests on the other hand are tests that need to utilize on the Android framework thus a device to run.

Hence, local tests run much faster than the instrumented tests as they do not require Android Device instrumentation. Use Instrumented tests only when writing integration and functional UI tests to automate user interaction, or when your tests have Android dependencies that mock objects cannot satisfy. For a complete read follow this link.

Writing your first local unit test.

Create new Project in Android Studio
Set Minimum SDK

Then choose Empty Activity Template from the next screen. After Android Studio finishes building your Project and Indexing, choose Run>Run App from the toolbar menu or hit F6 key, just to make sure everything is alright.

Lets now write a utility class for this demo. Lets say we need an App that does simple mathematics like addition, subtraction, multiplication and division. We will now write a SimpleMath.java class. Right click on your main package, com.example.javaand select New>New java class. Name it SimpleMath

package com.example.testdemo;

public class SimpleMath {

public int add(int op1, int op2){
return op1 + op2;

public int diff(int op1, int op2){
return op1-op2;

public double div(int op1, int op2) {
if (op2 == 0) return 0;
return op1 / op2;

I hope you understand what is going on until this point. The class above is fairly straightforward. Now lets proceed to writing our first unit test. Recall from above section Local Unit Test vs. Instrumented Tests, they are two different things which is also reflected in the directory structure. We have app\src\androidTest and app\src\test. We are going to have to put our Local Unit tests in the latter.

Before we begin, I want you to check your app level build.gradle file to see if you have the correct dependencies for setting up local unit tests. By default the android studio should have put those there but if not copy paste the following:
testImplementation 'junit:junit:4.12' or testCompile 'junit:junit:4.12' if you are running older gradle plugin.

Right click on your test folder (not androdTest folder) and click New>Java Class. Name it SimpleMathTest to reflect the unit test is for SimpleMath.java.

package com.example.testdemo;import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;public class SimpleMathTest {
private SimpleMath simpleMath;
@Before //This is executed before the @Test executes
public void setUp(){
simpleMath = new SimpleMath();
System.out.println("Ready for testing");
@After //This is executed after the @Test executes
public void tearDown(){
System.out.println("Done with testing");
public void testAdd() {
int total = simpleMath.add(4, 5);
assertEquals("Simple Math is not adding correctly", 9, total);
//The message here is displayed iff the test fails
public void testDiff() {
int total = simpleMath.diff(9, 2);
assertEquals("Simple Math is not subtracting correctly", 7, total);
public void testDiv(){
double quotient = simpleMath.div(9,3);
assertEquals("Simple math is not dividing correctly", 3.0, quotient, 0.0);
//@Ignore //This ignores the test below
public void testDivWithZeroDivisor(){
double quotient = simpleMath.div(9,0);
assertEquals("Simple math is not handling division by zero correctly", 0.0, quotient, 0.0);


Now as the comments indicate, @Before is executed before the actual @Test executes. We have instantiated the simpleMath object in the setUpmethod (function).

We have written 4 methods with @Test annotation, which means we have 4 unit tests that can either pass or fail. The testAddmethod tests the Addition functionality of SimpleMath. In this method we have used the simpleMath instance to add 4 and 5 and store it in an int total . In the next line assertEquals("Simple Math is not adding correctly", 9, total);we are checking if totalis or is not equal to 9, which it should be if our SimpleMath class is functioning correctly. The assertEquals() is one of the many static methods provided by the junit framework. It takes at least two parameters as the String parameter is optional which is displayed in case of test failure. The second and third parameters of the assertEquals method are expected and actual value in strict order.

Run the testAdd()

Running the testAdd() method

To run the test add either select the Green play button on the right of the line numbers or select the testAdd method right click and select Run testAdd(). If you run into error like TestSuite Empty which I hope you don’t, just click Run>Edit Configurations and Remove the test configurations under JUnit

Dealing with Test Suite Empty error

If everything goes well you should see something like the following:

testAdd() test Passed.

And you have now written your first local unit test in Android Application. Also notice how fast the test runs and how we did not need to use any device or emulator as the SimpleMath class does not require any Android Instrumentation or device features. The JVM on your development machine and the JVM on your phone is exactly the same and we can thus ensure the functionality that we just tested will work fine in an Android Device too.


Thank you and happy coding.

Android Application Developer. Interested in Open Source, TDD, Clean Code and Software Architecture