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

What's hot (20)

PPTX
Appium TestNG Framework and Multi-Device Automation Execution
pCloudy
 
ODP
Grails unit testing
pleeps
 
PDF
Unit test-using-spock in Grails
NexThoughts Technologies
 
PPTX
Grails Spock Testing
TO THE NEW | Technology
 
PDF
Introduction to web programming for java and c# programmers by @drpicox
David Rodenas
 
PDF
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
PPT
Java performance
Sergey D
 
PDF
Lab4
siragezeynu
 
PDF
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
KEY
Inside PyMongo - MongoNYC
Mike Dirolf
 
PPT
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
PPTX
Extending C# with Roslyn and Code Aware Libraries
Carlo Pescio
 
PDF
GMock framework
corehard_by
 
PDF
JUnit Pioneer
Scott Leberknight
 
PPTX
Unit testing patterns for concurrent code
Dror Helper
 
PPT
xUnit Style Database Testing
Chris Oldwood
 
PDF
Testing logging in asp dot net core
Rajesh Shirsagar
 
ODP
Decompiling Java - SCAM2009 Presentation
James Hamilton
 
PDF
Java programs
Mukund Gandrakota
 
PPT
Google C++ Testing Framework in Visual Studio 2008
Andrea Francia
 
Appium TestNG Framework and Multi-Device Automation Execution
pCloudy
 
Grails unit testing
pleeps
 
Unit test-using-spock in Grails
NexThoughts Technologies
 
Grails Spock Testing
TO THE NEW | Technology
 
Introduction to web programming for java and c# programmers by @drpicox
David Rodenas
 
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
Java performance
Sergey D
 
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
Inside PyMongo - MongoNYC
Mike Dirolf
 
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
Extending C# with Roslyn and Code Aware Libraries
Carlo Pescio
 
GMock framework
corehard_by
 
JUnit Pioneer
Scott Leberknight
 
Unit testing patterns for concurrent code
Dror Helper
 
xUnit Style Database Testing
Chris Oldwood
 
Testing logging in asp dot net core
Rajesh Shirsagar
 
Decompiling Java - SCAM2009 Presentation
James Hamilton
 
Java programs
Mukund Gandrakota
 
Google C++ Testing Framework in Visual Studio 2008
Andrea Francia
 

Similar to Generating characterization tests for legacy code (20)

PPTX
Generatingcharacterizationtestsforlegacycode
Carl Schrammel
 
PPT
Junit
Vivek Kulkarni
 
PPT
Working Effectively with Legacy Code (draft)
Andrea Francia
 
PDF
Characterization Testing : regaining control of your Legacy Code [EN]
Elapse Technologies
 
PPTX
Test-Driven Development Overview
Rob Myers
 
PPS
JUnit Goodness
Peter Sellars
 
PDF
Bad test, good test
Seb Rose
 
PPTX
Writing Good Tests
Matteo Baglini
 
ODP
Data-Driven Unit Testing for Java
Denilson Nastacio
 
PPT
Test Driven Development and JUnit
Somenath Mukhopadhyay
 
PPTX
Working effectively with legacy code
ShriKant Vashishtha
 
PPT
Tdd
guest446c0
 
PDF
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Tomek Kaczanowski
 
PPT
Code Tuning
bgtraghu
 
PPTX
The relationship between test and production code quality (@ SIG)
Maurício Aniche
 
PPT
Test Driven
Alex Chaffee
 
PPTX
A Test of Strength
Chris Oldwood
 
PDF
Unit testing best practices with JUnit
inTwentyEight Minutes
 
PPT
Refactoring
nkaluva
 
Generatingcharacterizationtestsforlegacycode
Carl Schrammel
 
Working Effectively with Legacy Code (draft)
Andrea Francia
 
Characterization Testing : regaining control of your Legacy Code [EN]
Elapse Technologies
 
Test-Driven Development Overview
Rob Myers
 
JUnit Goodness
Peter Sellars
 
Bad test, good test
Seb Rose
 
Writing Good Tests
Matteo Baglini
 
Data-Driven Unit Testing for Java
Denilson Nastacio
 
Test Driven Development and JUnit
Somenath Mukhopadhyay
 
Working effectively with legacy code
ShriKant Vashishtha
 
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Tomek Kaczanowski
 
Code Tuning
bgtraghu
 
The relationship between test and production code quality (@ SIG)
Maurício Aniche
 
Test Driven
Alex Chaffee
 
A Test of Strength
Chris Oldwood
 
Unit testing best practices with JUnit
inTwentyEight Minutes
 
Refactoring
nkaluva
 
Ad

More from Jonas Follesø (14)

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

Recently uploaded (20)

PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 

Generating characterization tests for legacy code

Editor's Notes

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