How to do TDD in Android ? Part 2 — Project architecture , Setup & Unit Testing

This tutorial will go through project architecture, setup from scratch and finally we will write some Unit tests.

In Why developers are scared to refactor code and Part 1 of this series we covered what is importance of automation test, what is testing pyramid, what are type of test, what tools can be use to do TDD in android and where the testing folders are located.


Project architecture

In accordance to automation testing we have to follow some kind of architecture pattern that will help us to test & structure our app in clean, testable manner.

There are many architecture pattern that easily support TDD Model–View–Presenter(MVP) , Model–View–Viewmodel(MVVM).

We are choosing MVP for our tutorial series.

  1. Model is typically POJO’s that are used by Presenter and View to convey information or to act upon.
  2. View (Activity or Fragment) is passive interface that displays data.This is place where we set data & make UI related changes like hiding , moving view.
  3. Presenter decides presentation logic , our business logic resides here. It talks to repository gets data, format it to display in view. It decides what should be displayed on View.Try avoiding use of Android specific code in presenter as much as possible (I know sometimes it’s harder 😉).

Project setup

In this post we will be creating simple android app from scratch . The app we will be creating is simple app that logins and take student attendance.

  1. Open Android Studio select “Start a new Android Project”
  2. Enter Application Name is “StudentAttendance”, Company Domain “nilesh.tdd.com” . Click Next

3. Select Android version you want support and Click “Next”

4. Select “Empty Activity” and click “Next”

5. Enter Activity Name and click “Finish”

6. Final project structure would look like below image


Unit Testing

After some refactoring of package structure, checkout commit (81d1668667bcb5eacf4cf94a8dffe738ad206d67)

Use below commands

git clone  https://github.com/NileshJarad/TDD_Demo.git
git checkout 81d1668667bcb5eacf4cf94a8dffe738ad206d67

If someone getting below error after checkout

Then change build.gradle version to 2.1.2 in project build.gradle

So finally we are here to get hands dirty on coding 😇.

Now we will write our first test case to check “If user tries to login after three attempt we will show him/her error message

Go to com.tdd.nilesh.studentattendance.login.LoginPresenter class. Cursor over class name and press SHIFT+CTRL+T (for mac SHIFT+CMD+T) this will pop up window with Create New Test option.
It will auto pick the class name for for Test and append Test at end of class name in our case LoginPresenterTest.
Click OK . It will again pop on window for which Test Folder. As we are writing JUnit select ../app/src/test/… it will create test class with same package structure.

I am posting snapshot of code as I want you to write a code. 😊
Now we will write Test as in below screen shot.

@Test annotation indicates it's test. As incrementLoginAttempt & isLoginAttemptExceeded are indicating in red colour. We must create method in LoginPresenter. Press ALT+ENTER that will create methods in presenter class as show below.

We have written failing test. Gradually we will write its implementation for it. To run a test, press small green play image left of test name.

As we written failing test output of test should be as below.
We are expecting 1 as output but we are getting 0.

Now we will write passing test implementation for test. Below is implementation for passing test.

Again run test it should be passing 😍.

Use below checkout command for Repo till now.

git checkout 302962ea6f63aa3afba003efa043ebacbaf02345

Now you must write your checkIfLoginAttemptIsNotExceeded test and check if your implementation is correct. If you not, don’t worry it will be in next commit.

Now We want to check “User name & password is correct

So we will write test and below is how it looks like (Failing test 😉)

Again repeat process for ALT+ENTER & run test. As it is failing test it will show red alert for failing.

Now we will write its implementation as below.

Run test again it will be passing now.

Now give your try for checkUsernameAndPasswordIsInCorrect test case .

Use below checkout command for Repo till now.

git checkout 4771fbfb8c6cbda98d9f01ac5e035ea02f0f5da6

Meanwhile , would like to hear suggestions & inputs on this.

Cheers 🍻 !!!.

Gently hit ♥️ below.

Checkout Next post :