[TDD] Test Driven Development — with Java and JUnit

Prabhash
5 min readAug 16, 2021

You have landed on this blog, I am assuming that you wanted to learn and get started with Test Driven Development with Java and JUnit…

We will start with some definitions and then move to code, if you want you can directly move to code. I have added github url of the example used in this story at the bottom.

TDD — each one of us like to include words like TDD, DDD, Microservices Architecture in our resume, however before doing that ask yourself this. ‘Are you really using these practices in your daily coding activities’. ‘Are you really writing test cases first before even creating the actual implementation class for any feature development’.

Our favorite Uncle Bob defines TDD with 3 rules (more details here):

1. You must write a failing test before you write any production code.

2. You must not write more of a test than is sufficient to fail, or fail to compile.

3. You must not write more production code than is sufficient to make the currently failing test pass.

These three laws are like nano-cycle of TDD, works like second-on-second basis.

The cycle of TDD goes like : (Red -> Green -> Refactor) -> Repeat

TDD Cycle (Image Source)

What is TDD ?

--

--

Prabhash

A Software Engineer who loves solving problems and watches tons of TV Series!