Test Driven Development Is Not Hard
Published: 2020-06-09 08:23:49

Have you ever tried building an application from scratch?

How did you start writing your code, building your module and making sure that everything that you do works?

Well, if you have never tried test driven development before, you wouldn't understand the good part it brings in developing your code.

As I always say - TDD is not a testing process, it is a design process. 

So, if you think TDD is hard then you need a fresh perspective. Well, this article will give you the starting point from where you can learn and master the skill.

## Create A Simple Password Validator Using TDD

Statement: Create a Password Validator that would take any string and perform length check.

- Length should not be smaller than 3 and greater than 5

For brevity I will just stick to one condition. The length of the input provided.

Let's start with the requirement and build our test cases accordingly.

From the problem statement, it is pretty clear that we have to build a PasswordValidator. So, let's create a `PasswordValidatorTest` class.

```
class PasswordValidatorTest {
    
    PasswordValidator passwordValidator;

    @Before
    public void setUp()
    {
         passwordValidator = new PasswordValidator();    
    }
}
```

This will make sure that `PasswordValidator` class object is created before every test cases.

### Identify the test cases

In the problem statement, we can clearly see that the length of the input string should not be **smaller than 3 and greater than 5**.

The problem is a two part problem, first it talks about a lower limit and then upper limit. We will consider one scenario at a time.

So, our first test case will be to cover the first condition. That is the length of the input string should be greater than three.

```
@Test
public void itShouldReturnTrueIfThePasswordLengthIsGreaterThan3Characters()
{
    String testString = "abcdefgh";
    boolean flag = passwordValidator.isValid(testString);
    Assert.assertTrue(flag);
}
```

In this test cases, I expect my `PasswordValidator` class to have a `.isValid(String password)` function and will return a boolean based on the condition met.

Now since we have the test case ready, we will go back to our class and write just enough code to make this test pass.

```
public boolean isValid(String password)
{
   if (password.length() > 3)
       return true;
   return false;
}
```

Ta Da!!! 

Test should pass now.

Let's tackle the second condition. The __Password's length should not be greater than 5__

We will write another test case where we would expect our function to return false if the input length is greater than 5.

```
@Test
public void itShouldReturnFalseIfthePasswordLengthIsGreaterThan5()
{
    String testString = "abcdefgh";
    boolean flag = passwordValidator.isValid(testString);
    assertFalse(flag);
}
```

The above test will make sure that the length of the input is less than 5.

But one important aspect of Test Driven Development is that your all the test cases must pass at all times.  Otherwise you are not doing it correct.

So, let go back to the main class and write just enough code to make it a pass.

```
public boolean isValid(String password)
{
   if (password.length() > 3)
       return true;
   if (password.length() > 5)
       return false;
   return false;
}
```

Once you are done with all the scenarios, it is time to revisit your code and try to optimize it. 

You see, here we are returning false twice. One way to eliminate this is to combine both the condition together in one statement.

```
public boolean isValid(String password)
{
   String length = password.length();
   if (length > 3 && length < 5)
       return true;
   return false;
}
```

[Example Source](https://www.bemyaficionado.com/mindset-behind-test-driven-development)

Great!

Do you see any other point of improvement in the above code.

Another rule of thumb is to not stress over improvisation. Give it 30 seconds, if you do not see a way to optimize it then just move on to the next test case.

Well, isn't it simple.

If you want to read more about Test Driven Development then here are some good sources for that - 

- [Introduction to Test Driven Development](http://agiledata.org/essays/tdd.html)
- [Test Driven Development is the best thing that has happened to Software Development](https://www.thoughtworks.com/insights/blog/test-driven-development-best-thing-has-happened-software-design)
- [Mindset Behind Test Driven Development](https://www.bemyaficionado.com/mindset-behind-test-driven-development) (Recommended)

Hope you liked the article.

See you next time.