@andreafrancia
Test-Driven Development
su Codice Esistente
Andrea Francia
5 aprile 2018 @ Crafted Software Milano
@andreafrancia
The conservative approach
No automatic tests
leads to
Manual testing
that leads to
High risk of introduce bugs
that leads to
Fear of change
so
You prefer add more mess instead cleaning up the existing code
and
the code get worse,
the cost/time of adding features increases
If it ain't broke don't fix it
@andreafrancia
The Legacy Code Dilemma
When we refactor, we should have tests.
To put tests in place, we often have to refactor
@andreafrancia
Trip Service Kata
• Sandro Mancuso (@sandromancuso)
• https://github.com/sandromancuso/trip-service-
kata
• http://www.slideshare.net/MozaicWorks/sandro-
mancuso-testing-and-refactoring-legacy-code
@andreafrancia
Requisiti di business
Considerate un social network per viaggiatori.
• Devi essere loggato per vedere i contenuti del
sito.
• Devi essere un amico per poter vedere i viaggi di
un altro utente.
@andreafrancia
Le regole del legacy code
• Puoi modificare solo il codice di produzione che è
coperto dai test
• (al massimo puoi usare solo i safe refactoring nel
caso siano necessari per mettere il codice sotto
test)
@andreafrancia
Dipendenze
TripService)
+findTripsByUser(User)0:0List<Trip>0
<<Singleton>>)
UserSession)
+getInstance()0:0UserSession0
+getLoggedUser()0:0User0
TripDAO)
+findTripsByUser(User)0:0List<Trip>0
http://www.slideshare.net/MozaicWorks/sandro-mancuso-testing-and-refactoring-legacy-code/7
@andreafrancia
Start	refactoring	from	
deepest	to	shortest	
branch	
	
Start	tes3ng	from	
shortest	to	
deepest	branch	
	
Working	with	Legacy	Code	Tips	
This slide is taken from a Sandro Mancuso Slideshare: 

https://www.slideshare.net/sandromancuso/legacy-code-handson-session/7
@andreafrancia
Demo time
@andreafrancia
Grazie
Andrea Francia
@andreafrancia

Test-Driven Development su Codice Esistente