Unit Testing

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

Use below commands



git checkout 81d1668667bcb5eacf4cf94a8dffe738ad206d67 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 :