Android Unit Testing Fundamentals

Sunny Sultan
3 min readMar 15, 2019
The Testing Pyramid, showing the three categories of tests that you should include in your app’s test suite

Testings are a very crucial part of developing a system. The unit test is the first step of System Testing. Developers usually do unit testing when they write a program and before the production code.

Unit Testing ensures that a class or a method does what it’s supposed to do.

Unit tests are the fundamental tests in an app testing strategy. By creating and running unit tests against your code, you can easily verify that the logic of individual units is correct.

In this section, we will test our code by JUnit. JUnit is the unit testing framework of Java.

Let's get Started….

First, create an empty Android project and create a new class PositiveNumberValidator.

public class PositiveNumberValidator {
public boolean isPositive(int number) {
return number >= 0;
}
}

This class holds a method which aim is to get a number and check the number positive or negative. if positive it will return true otherwise false.

If we want to test the method we need to create a test class for this. So, put your cursor on the class PositiveNumberValidator class and press CTRL+SHIFT+T then enter new test class and it will pop up a window, just click Ok.

Now your test class created under Test Package section. and it should look like this.

import static org.junit.Assert.*;

public class PositiveNumberValidatorTest {

}

Now at first, we need to create our PositiveNumberValidator class object from PositiveNumberValidatorTest class. For creating Object we need to generate a setup method in Test class.

public class PositiveNumberValidatorTest {

PositiveNumberValidator SUT;

@Before
public void setup() {
SUT = new PositiveNumberValidator();
}

}

This setup method will call before the Test methods are Execute.

Now its time to create some test methods. Ok, lets started...

public class PositiveNumberValidatorTest {

PositiveNumberValidator SUT;

@Before
public void setup() {
SUT = new PositiveNumberValidator();
}

@Test
public void test1() {
boolean result = SUT.isPositive(-1);
Assert.assertThat(result, is(false));
}

@Test
public void test2() {
boolean result = SUT.isPositive(0);
Assert.assertThat(result, is(true));
}

@Test
public void test3() {
boolean result = SUT.isPositive(1);
Assert.assertThat(result, is(true));
}
}

I created some methods here Test1, Test2, Test3. For simplicity, I named them like these. You obviously use a meaningful name.

I use assertThat to check the method return true or false. In the Test1 method, we sent -1 and we know that -1 is not positive so we will get a false return from the result. So we put false in is() function. If the test pass, that means, in that case, our code is ok.

Now press CTRL+SHIFT+F10 and run the Test. Voila! Our all three Test methods are run successfully. That means there are no bugs in our isPositive method.

Now create some bug in our isPositive method for study purpose.

public boolean isPositive(int number) {
return number > 0;
}

Now go to the Test class and re-run the test by pressing CTRL+SHIFT+F10 and this time Test2 will fail. Because we put a bug there. and Now you need to fix the bug.

That’s it. In the future, I will write more about Android Unit Testing. Till then be happy, be healthy.

--

--