SlideShare a Scribd company logo
Generating Characterization Tests for Legacy CodeJonas Follesø (@follesoe)JavaZone 2010, 09. September
Huge methods (~3000+ lines)
Dave & Karin http://www.flickr.com/photos/dnk_uk/3525103502/~50 slow integration tests
How the development team felt...
What they needed
What I read
Legacy code is code without tests.  Code without tests is bad code. -Michael C. Feathers Fraser Speirs http://www.flickr.com/photos/fraserspeirs/3395595360/
A characterization test is test that characterizes the actual behavior of a piece of code. It acts as a change detector, protecting legacy code form unintended changes
publicdoubleCalc(doubleinv, doublert, inty){doubleret=0;for (inti=1; i<=y; i++)    {ret=inv*Math.Pow(1.0+rt/100.0, i);    }returnret;}
[TestMethod]publicvoidCalc_characterization(){varcalc=newCalcUtil();doubleresult=calc.Calc(10000, 10, 10);Assert.AreEqual(42.0, result);            }
Assert.AreEqual failed. Expected:<42>. Actual:<25937.424601>.
[TestMethod]publicvoidCalc_characterization(){varcalc=newCalcUtil();doubleresult=calc.Calc(10000, 10, 10);    Assert.AreEqual(42, result);                Assert.AreEqual(25937.424601, result);            }
Test run completed. Results 1/1 passed.
publicdoubleCalculateCompoundInterest(doubleinvestment, doubleinterest, intyears){doubleprojectedValue=0.0;for (intyear=1; year<=years; year++)    {projectedValue=investment*Math.Pow(1.0+interest/100.0, year);    }returnprojectedValue;}
…A pinch point is a natural encapsulation boundary. When you find a pinch point, you’ve found a narrow funnel for all the effects of a large piece of code…-Michael C. Feathers Fraser Speirs http://www.flickr.com/photos/fraserspeirs/3395599536/
~85% code coveragehttp://www.flickr.com/photos/shuttercat7/713186211/
// This method is the "pinch point" we want to test...publicobjectCalculateSomething(objectsomeParameter){// 1) Record input parameters...// 2) Do the work...// 3) Write parameters and return value to XML file.}
[TestMethod]publicvoidCalculateSomething_test1(){Run("Recordings/CalculateSomething1.xml");}[TestMethod]publicvoidCalculateSomething_test2(){Run("Recordings/CalculateSomething2.xml");}publicvoidRun(stringfilename){// Load input parameters from XML file// Load return value from XML file// Call method under test// Use reflection to compare actual vs. recorded}
~300 fast characterization tests
How can we reuse this?
http://follesoe.github.com/BlackBoxRecorder
[Recording]
[Dependency]
[Recording]publicList<EmployeeEntity>GetMakingMoreThan(doublesalary){vardal=newEmployeeDAL();varemployees=dal.GetAllEmployees();returnemployees.Where(e=>e.Salary>salary).ToList();}
[Dependency]publicclassEmployeeDAL{publicList<EmployeeEntity>GetAllEmployees()    {// Calls to database...    }}
<Recording>  <Name>GetEmployeesMakingMoreThan_salary</Name>  <Method>GetEmployeesMakingMoreThan</Method>  <Type><![CDATA[BlackBox.Demo.App.SimpleAnemic.EmployeeBL]]></Type><InputParameters>    <Parameter>      <Name>salary</Name>      <Type><![CDATA[System.Double]]></Type>      <Value><![CDATA[5000]]></Value>    </Parameter>  </InputParameters>  <Return>    <Type><![CDATA[List<BlackBox.Demo.App.SimpleAnemic.EmployeeEntity>]]></Type>    <Value><![CDATA[XML representation of employees retruned from method]]></Value>  </Return>  <Dependencies>    <Dependency>      <Type><![CDATA[BlackBox.Demo.App.SimpleAnemic.EmployeeDAL]]></Type><Method>        <Name>GetAllEmployees</Name>        <ReturnValues>          <ReturnValue><Value><![CDATA[XML representation of all employees returned from db]]><Value>          </ReturnValue>        </ReturnValues>      </Method>    </Dependency></Dependencies></Recording>
[TestMethod]publicvoid GetEmployeesMakingMoreThan_salary(){    Run(@"GetEmployeesMakingMoreThan_salary.xml");}
http://github.com/oc/jackbox
@RecordingpublicintexampleMethod(intparameter, intparameter2) {returnparameter+parameter2;}@DependencypublicStringinvokedMethodOnDependency(Stringargument) {returnargument.toUpperCase();}
Jonas FollesøSenior Consultant+47 977 06660Jonas.folleso@bekk.no / jonas@follesoe.no

More Related Content

PPTX
TDD? Sure, but What About My Legacy Code?
PPTX
Migrating to JUnit 5
PPT
Introduzione al TDD
PPTX
Unit testing concurrent code
PDF
An introduction to Google test framework
PPTX
Automation patterns on practice
PDF
C++ Unit Test with Google Testing Framework
PPTX
Code generation for alternative languages
TDD? Sure, but What About My Legacy Code?
Migrating to JUnit 5
Introduzione al TDD
Unit testing concurrent code
An introduction to Google test framework
Automation patterns on practice
C++ Unit Test with Google Testing Framework
Code generation for alternative languages

What's hot (20)

PPTX
Appium TestNG Framework and Multi-Device Automation Execution
ODP
Grails unit testing
PDF
Unit test-using-spock in Grails
PPTX
Grails Spock Testing
PDF
Introduction to web programming for java and c# programmers by @drpicox
PDF
Java Programming - 03 java control flow
PPT
Java performance
PDF
PDF
33rd Degree 2013, Bad Tests, Good Tests
KEY
Inside PyMongo - MongoNYC
PPT
2012 JDays Bad Tests Good Tests
PPTX
Extending C# with Roslyn and Code Aware Libraries
PDF
GMock framework
PDF
JUnit Pioneer
PPTX
Unit testing patterns for concurrent code
PPT
xUnit Style Database Testing
PDF
Testing logging in asp dot net core
ODP
Decompiling Java - SCAM2009 Presentation
PDF
Java programs
PPT
Google C++ Testing Framework in Visual Studio 2008
Appium TestNG Framework and Multi-Device Automation Execution
Grails unit testing
Unit test-using-spock in Grails
Grails Spock Testing
Introduction to web programming for java and c# programmers by @drpicox
Java Programming - 03 java control flow
Java performance
33rd Degree 2013, Bad Tests, Good Tests
Inside PyMongo - MongoNYC
2012 JDays Bad Tests Good Tests
Extending C# with Roslyn and Code Aware Libraries
GMock framework
JUnit Pioneer
Unit testing patterns for concurrent code
xUnit Style Database Testing
Testing logging in asp dot net core
Decompiling Java - SCAM2009 Presentation
Java programs
Google C++ Testing Framework in Visual Studio 2008
Ad

Similar to Generating characterization tests for legacy code (20)

PPTX
Generatingcharacterizationtestsforlegacycode
PPT
PPT
Working Effectively with Legacy Code (draft)
PDF
Characterization Testing : regaining control of your Legacy Code [EN]
PPTX
Test-Driven Development Overview
PPS
JUnit Goodness
PDF
Bad test, good test
PPTX
Writing Good Tests
ODP
Data-Driven Unit Testing for Java
PPT
Test Driven Development and JUnit
PPTX
Working effectively with legacy code
PPT
PDF
Sample Chapter of Practical Unit Testing with TestNG and Mockito
PPT
Code Tuning
PPTX
The relationship between test and production code quality (@ SIG)
PPT
Test Driven
PPTX
A Test of Strength
PDF
Unit testing best practices with JUnit
PPT
Refactoring
Generatingcharacterizationtestsforlegacycode
Working Effectively with Legacy Code (draft)
Characterization Testing : regaining control of your Legacy Code [EN]
Test-Driven Development Overview
JUnit Goodness
Bad test, good test
Writing Good Tests
Data-Driven Unit Testing for Java
Test Driven Development and JUnit
Working effectively with legacy code
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Code Tuning
The relationship between test and production code quality (@ SIG)
Test Driven
A Test of Strength
Unit testing best practices with JUnit
Refactoring
Ad

More from Jonas Follesø (14)

PPTX
Introduction to F#
PPT
Hvordan lage en vellykket Windows Phone 7 App
PPTX
Introduction to MonoTouch
PPTX
Hvordan lage en vellykket WP7 applikasjon
PPTX
Why learn new programming languages
PPTX
Smidig 2011 TDD Workshop
PPTX
Cross platform mobile apps using .NET
PPTX
An overview of the Windows Phone 7 platform
PPTX
Windows Phone 7 lyntale fra Grensesnittet Desember 2010
PPTX
NNUG Trondheim 30.09.2010 - Windows Phone 7
PPTX
Get a flying start with Windows Phone 7 - NDC2010
PPTX
Smidig brukeropplevelse med skjermbildeprototyper (Smidig2009)
PPTX
MVVM Design Pattern NDC2009
PPT
Silverlight 2 for Developers - TechEd New Zealand 2008
Introduction to F#
Hvordan lage en vellykket Windows Phone 7 App
Introduction to MonoTouch
Hvordan lage en vellykket WP7 applikasjon
Why learn new programming languages
Smidig 2011 TDD Workshop
Cross platform mobile apps using .NET
An overview of the Windows Phone 7 platform
Windows Phone 7 lyntale fra Grensesnittet Desember 2010
NNUG Trondheim 30.09.2010 - Windows Phone 7
Get a flying start with Windows Phone 7 - NDC2010
Smidig brukeropplevelse med skjermbildeprototyper (Smidig2009)
MVVM Design Pattern NDC2009
Silverlight 2 for Developers - TechEd New Zealand 2008

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction

Generating characterization tests for legacy code

Editor's Notes

  • #29: Johannes BroadwallOle Christian RynningMarius B.KotsbakGeir Amdal