TDD Kata in Java inspired by Matteo Vaccari
- I implemented it following baby steps
- Of course using the TDD cycle: Red --> Green --> Refactor
- My approach to write functional tests for TDD can be found here is a diagram showing how tests interacts only with Domain Objects and never with external dependencies.
TDD does not mean you will end up with a good design. A good designer will likely come up with a good design. That said, I've listed couple of heurists:
-
Avoid usage of null, instead use Null Object pattern (you can see this on class
NullEmployee
). -
Always run all tests when you're refatoring.
-
Test should have full control of everything (time, date, test data, etc). Please check class
BirthdayGreetingsTestObjects
and how it is used on Tests. -
Never hardcode today() or any time/date inside your classes. It is better to pass it as parameter. Please check method
greetOn(LocalDate date)
in classBirthdayGreeting
. Even test objects you can make them flexible on time/date like methodemployeeSystemOfRecordsWithTwoEmployeeBornOn(LocalDate date)
-
Pass dependencies in constructors. Please check constructor
BirthdayGreeting(EmailSystem emailSystem, EmployeeSystem employeeSystemOfRecords)
-
Use names which represents better its meaning e.g:
EmployeeSystemOfRecord
is prefered overEmployeeRepository
andBirthdayGreeting
is prefered overBirthdayService
-
Make your classes to depend on Interfaces
public BirthdayGreeting(EmailSystem emailSystem, EmployeeSystem employeeSystemOfRecords){
this.emailSystem = emailSystem;
this.employeeSystemOfRecords = employeeSystemOfRecords;
}
- Avoid Mocking frameworks as much as you can. Instead use a technique shown in class
BirthdayGreetingsTestObjects
public EmailSystem successEmailSystemWithTrue() { return (email) -> {return true;}; }
public EmailSystem successEmailSystemWithFalse() { return (email) -> {return false;}; }
-
Avoid naming classes like
YYYManager
,YYYController
,EmployeeValidator
, -
You will get the most of TDD if you create functional tests (not unit tests nor integration tests).
- Unit tests are very fine grained (e.g: EmployeeTest where you will test only Employee class isoleted).
- Integration tests depend on I/O and usually are quite slow and unpredictable.
Both (unit and integration tests) could be important for your project, but they are not part of pure hardcore TDD :-)
- Rember a good developer is also a good tester. Be proffesional and responsible, always develop robust software following best practices. It will save you time, headaches, pager duties, issues in production, etc.
- Music to keep you in the zone radio de x-team
- Adrian Bolboaca blog with tons of good quality videos doing TDD.
- Matias Tripode - My profile on Linkedin and on twitter
This project is licensed under the MIT License - see the LICENSE.md file for details
- Hernan Wilkinson and 10pines for sharing such amazing knowledge