SlideShare a Scribd company logo
TDD 101
3. Testing Legacy code
Testing Legacy code
Legacy is 

code without tests
Michael Feathers
Refactor without tests
Why refactor
without tests
Reflect knowledge about the system

Put the system in a testable state
How to refactor
without tests
Perform safe refactors

Use tools provided by IDE

Proceed step by step
Leave the camp better 

than you found it
The Camp Rule
Rename Rename things to reflect your current
knowledge of the system

Give meaning to them
$message
$confirmationMessage
public function processFile($file)
public function extractInvoiceIdsFromFile($file)
Extract
constants
Give meaning to magic values
if ($items >= 3)
if ($items >= self::MINIMUN_ITEMS_TO_SHOW)
Extract variables Simplify complex expressions slicing
them into parts

Give meaning to them
$amount = $price - ($price * $pct / 100);
$discount = $price * $discountPercent / 100;
$amount = $price - $discount;
Extract method Isolate dependencies while not ready to
inject them

Give meaning to them
$date = new DateTime();
$date = $this->getCurrentDate();
public function getCurrentDate()
{
return new DateTime();
}
Extract method Simplify complex operations slicing them
into parts based on abstraction level

Give meaning to them
public function doSomething()
{
// more than 30 lines of code…
}
public function doSomething()
{
$this->prepareThings();
$this->getData();
$this->processData;
return $result;
}
Extract interface Abstract behaviors so you can 

replace/exchange implementations

Give meaning to them
class SomeService
{
public function doSomething(): Class
{
}
}
interface Service
{
public function doSomething(): Class;
}
class SomeService implements Service
class BetterService implements Service
Are you getting the point?
Refactor is about 

knowledge and meaning
Testing legacy code
We need
characterization
tests
Describe the behavior of a system/unit
from the outside

Have a base line
Characterization
tests
Get the output of a unit of software

Use that to create a test
Characterization
tests
Hypothesis about the behavior 

of a unit of code

Make a test to prove it
Fighting dependencies
The first step on the way to
victory is to recognize the enemy
Corrie Ten Boom
Dependencies Identify injectable dependencies

Isolate them (extract method)

Extract them (extract class, extract
interface)
Injectables Don’t need runtime knowledge at
construction time

Can have dependencies on another
injectables

You can use the same instance
Newables Need runtime knowledge to be created

Don’t have injectable dependencies

You need different instances
Hidden
dependencies
Those “new” inside the code

Use extract method to isolate them

Override method to test

Isolate, Inject
Global state
dependencies
Dependency on things out of our system 

• Ramdomness (uuid)

• DateTime

• File System…

Isolate, Extract, Inject
Takeaways
Legacy is code without tests

Refactor to 

• reflect (evolving) knowledge about the
system

• put system in a testable state

Describe behavior with tests

Fight hidden and global dependencies
Hands on…
1. Make this thing run again
2. Put in testable state
3. Describe behavior with tests
4. Fix the @todo, test protected

More Related Content

PPTX
UnitTestingBasics
PDF
Unit testing best practices
PPTX
Unit testing
PPT
Unit testing
PPTX
Effective Readable unit testing with junit5
PPTX
Writing Test Cases with PHPUnit
PPTX
Introduction to JUnit
PPTX
Stored procedures
UnitTestingBasics
Unit testing best practices
Unit testing
Unit testing
Effective Readable unit testing with junit5
Writing Test Cases with PHPUnit
Introduction to JUnit
Stored procedures

What's hot (12)

PPT
Junit4.0
PPTX
TestNG Data Binding
PPT
PPT
Software testing basics and its types
PDF
JUnit & Mockito, first steps
PPT
Junit and testNG
PPTX
Test ng tutorial
PPT
PDF
Introducing generic types
PPTX
TestNG Session presented in PB
PPTX
Unit testing
Junit4.0
TestNG Data Binding
Software testing basics and its types
JUnit & Mockito, first steps
Junit and testNG
Test ng tutorial
Introducing generic types
TestNG Session presented in PB
Unit testing
Ad

Similar to Testing legacy code (20)

PPTX
Testing the untestable
PDF
Working Effectively with Legacy Code: Lessons in Practice
PDF
Refactoring legacy code
PDF
Unit testing legacy code
PDF
Working With Legacy Code
PDF
Characterization Testing : regaining control of your Legacy Code [EN]
PPTX
Working with Legacy Code
PDF
Refactoring Legacy Code
PPTX
Taming scary production code that nobody wants to touch
PPTX
Refactoring workshop
PDF
Ddc2011 효과적으로레거시코드다루기
PDF
Legacy is Good
PPTX
Working Effectively with Legacy Code
PPTX
Test Drive Dirven Driver HAHAahhaha.pptx
PPTX
Escape the legacy code matrix - Vimercate
PPTX
Refactoring & Testing software.
PPTX
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
PDF
So You Just Inherited a $Legacy Application… NomadPHP July 2016
PDF
Reduce Reuse Refactor
PDF
So You Just Inherited a $Legacy Application...
Testing the untestable
Working Effectively with Legacy Code: Lessons in Practice
Refactoring legacy code
Unit testing legacy code
Working With Legacy Code
Characterization Testing : regaining control of your Legacy Code [EN]
Working with Legacy Code
Refactoring Legacy Code
Taming scary production code that nobody wants to touch
Refactoring workshop
Ddc2011 효과적으로레거시코드다루기
Legacy is Good
Working Effectively with Legacy Code
Test Drive Dirven Driver HAHAahhaha.pptx
Escape the legacy code matrix - Vimercate
Refactoring & Testing software.
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
So You Just Inherited a $Legacy Application… NomadPHP July 2016
Reduce Reuse Refactor
So You Just Inherited a $Legacy Application...
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PPTX
history of c programming in notes for students .pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administration Chapter 2
PDF
System and Network Administraation Chapter 3
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Essential Infomation Tech presentation.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
Nekopoi APK 2025 free lastest update
history of c programming in notes for students .pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administration Chapter 2
System and Network Administraation Chapter 3
wealthsignaloriginal-com-DS-text-... (1).pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Operating system designcfffgfgggggggvggggggggg
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Softaken Excel to vCard Converter Software.pdf
Digital Strategies for Manufacturing Companies
CHAPTER 2 - PM Management and IT Context
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Essential Infomation Tech presentation.pptx
How Creative Agencies Leverage Project Management Software.pdf

Testing legacy code