Hello!
It feels like yesterday that we asked โWhat is TDD?โ in Part 1, and here we are at the final installment.
Starting from an empty project, we built a solid application that registers and retrieves products, validates input, and handles exceptions.
In this last post, we'll revisit what we learned and see how this
habit
can serve you throughout your career as a developer.
We learned the RedโGreenโRefactor cycle at the heart of TDD and wrote our first tests with JUnit 5 and AssertJ.
In particular, we learned the value of writing the
minimum implementation
that gets the test to pass.
Improving structure and testing each layer (Parts 5โ8)
We
refactored
messy code to separate the service and repository responsibilities.
We switched from a fake in-memory store to
JPA
and tested the controller with
MockMvc
.
Advanced techniques and problem-solving (Parts 9โ13)
We used
Mockito
to isolate dependencies and verify behavior.
We validated inputs with @Valid and made time-dependent code testable using a
Why did we go to all the trouble of writing these tests?
The payoff becomes clear after development is complete.
Refactoring with confidence
Changing code no longer means wondering whether something else will break.
When the tests give us a
green light
, we can improve the structure with confidence.
Living documentation
Tests are often more accurate than a complicated wiki.
If you want to know which inputs make an API return an error, the tests give you an answer you can verify.
Faster debugging
When a bug occurs, a failing test can show you where to look without searching the entire codebase.
It's like having security cameras in every corner of the code.
We used H2 as an in-memory database, but on a real project you may want to test against the same MySQL or Redis environment you use in production.
Try learning
Testcontainers
, which uses Docker.
BDD (Behavior-Driven Development)
Tools such as
Cucumber
and
JGiven
let you write tests in language closer to a requirements document, using Given, When, and Then in place of technical assertions such as assertEquals.
Performance testing
Once you know the feature behaves correctly, check how fast it runs.
Try load testing with
JMeter
or
nGrinder
.
CI/CD pipelines
Set up
GitHub Actions
or
Jenkins
to run your tests automatically whenever you push code to GitHub, so they run outside your own computer too.
A test is the first client code a developer writes.
That's one of my favorite sayings.
With TDD, you are the first person to use your own code through tests. That lets you fix awkward parts and improve the design as you go.
It can feel slow and frustrating at first.
Keep practicing and you will find yourself developing more confidently with tests alongside your code.
By then, you'll already have become a
better developer
.
Thank you for following this long series.
I hope your tests keep giving you green lights!