Android Unit Test Tutorial – Writing Your First Unit Test

Hey folks, so you can write or code android apps. But do you test them? Yeah, I know everyone compiles and sees the app, whether it is working or not. But do you write code to test? Do you test every function and every module?. If your answer is NO then, this Android Unit Test Tutorial is for you.

I guess now you want to write tests for your app, and if you are not sure where to start, then not to worry; I will be covering everything related to testing in this series of posts.

Now before moving ahead, you should go through this video to understand about testing in android development.

In this post, you will learn to write your first Unit Test. So without wasting any more time, let’s start.

What is Unit Testing?

Unit testing is a testing methodology where we test individual units (or the smallest unit) of application code; to ensure they are working properly.

Unit testing helps to confirm that every single unit of the application’s code is working as expected.

Often unit tests are written even before writing the code. In other words, for every function that is to be created, a unit test is written before writing the function. By following this approach, we can fix the possible bugs in the early stage of development.

Benefits of Unit Testing

Unit Test,

  • Helps in finding bugs early.
  • As it helps to find the bugs in the early stage of development and reduces the development cost and time.
  • Simplifies refactoring and provides documentation.

A unit test is not specific to any platform like Android or Web. It is a concept that is used in software development. But here, our focus is performing Unit Tests in our Android Projects.

Android Unit Test Tutorial

Now, let’s discuss our main topic that is Android Unit Test.

Whenever you create an android project, you see these three packages.

Android Unit Test Tutorial
Android Unit Test Tutorial

You can see you have the main package (the first one), and inside this package, we put all our application’s code. But with this package, we have two more packages. You can differentiate these packages with shown hint androidTest & test.

We will not discuss androidTest now. Our aim today is only the test package. Inside this package, we write all the code for Unit Test.

Now go to app-level build.gradle  file and see the dependencies block.

You can see here we have testImplementation  and androidTestImplementation . testImplementation are the libraries available inside test package. And the same way if you want the library to be available inside androidTest package you need to use androidTestImplementation.

Now, the Java Unit Testing Framework JUnit is added by default here. Whenever you create any project it is added by default. We will use JUnit for unit testing our code.

But with JUnit we will use Google Truth, to simplify the assertions. As it is not available by default; we need to add it inside the dependency block of app level build.gradle file.

Code for Unit Testing

For this example, I have a very simple function that we will unit test.

It is a very simple code. This function will validate the given parameters. Now the rules here are

  • The given amount should be greater than zero and.
  • Given desc should not be blank.

If the given parameters satisfy the conditions, we will return true, which means inputs are valid. For any other case, we will return false, which means the inputs given are not valid.

Now we need to test this function to make sure this function is working as expected.

And to test this function, we will create a test file inside the test package. You can create the file manually, or you can follow these steps.

  • Right-click on the class that needs to be tested.
  • Go to generate and then select Test.
  • Now select JUnit4 and hit ok.

Writing Unit Test

Now finally we will unit test our validateInput function.

Now here we have the class; that will contain all the functions to test the Validator . Every unit test class needs to be annotated with @RunWith(JUnit4::class)  ( JUnit4::class  because we are using JUnit4, you can change it with whatever version you want to use).  Inside this class, we will write test functions to test all the scenarios. As this is just an example, we have only two test cases. One is for valid input, and another one is for invalid input.

Another thing to note is, you need to annotate all your test functions with @Test . Now here you can see I have two functions.

whenInputIsValid() : this function is to test the case, when we will provide valid input to our function. So what we are doing here is, we are just calling the validateInput()  function and passing a valid input. Now we are asserting that the returned value is true.

whenInputIsInvalid() : here also we are doing the same thing as above; the only difference is we are passing invalid input this time and we are asserting that the result equals false.

To run the test you can use the play buttons in the Android Studio. You will see the play button before every function and class.

Android Unit Test Tutorial 1
Android Unit Test Tutorial

Just run the test, and you will see it passing the test. If the test fails, you need to make changes to your function.

So whenever you change anything in the function, you need to test it again. And this way, your code will not break with future changes because it must always pass the test first.

Android Unit Test Tutorial Source Code

I am building a complete testing series, and in this series, we are building an application using the Test Driven Development approach. And to be ready for the next lesson, you should get the project’s source code from here.

So that is all for this post; stay tuned for the coming tutorials as we will cover a lot of things related to Testing in Android Development. Thank You 🙂

Leave a Comment