SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Generating Characterization Tests for Legacy Code Jonas 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 coverage http://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
@Recording publicintexampleMethod(intparameter, intparameter2) { returnparameter+parameter2; } @Dependency publicStringinvokedMethodOnDependency(Stringargument) { returnargument.toUpperCase(); }
Jonas Follesø Senior Consultant +47 977 06660 Jonas.folleso@bekk.no / jonas@follesoe.no

Weitere ähnliche Inhalte

Was ist angesagt?

Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 

Was ist angesagt? (20)

Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicox
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
 
Java performance
Java performanceJava performance
Java performance
 
Lab4
Lab4Lab4
Lab4
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYC
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Extending C# with Roslyn and Code Aware Libraries
Extending C# with Roslyn and Code Aware LibrariesExtending C# with Roslyn and Code Aware Libraries
Extending C# with Roslyn and Code Aware Libraries
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
 
Testing logging in asp dot net core
Testing logging in asp dot net coreTesting logging in asp dot net core
Testing logging in asp dot net core
 
Decompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 PresentationDecompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 Presentation
 
Java programs
Java programsJava programs
Java programs
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 

Ähnlich wie Generating characterization tests for legacy code

Generatingcharacterizationtestsforlegacycode
GeneratingcharacterizationtestsforlegacycodeGeneratingcharacterizationtestsforlegacycode
Generatingcharacterizationtestsforlegacycode
Carl Schrammel
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
the next web now
the next web nowthe next web now
the next web now
zulin Gu
 

Ähnlich wie Generating characterization tests for legacy code (20)

Generatingcharacterizationtestsforlegacycode
GeneratingcharacterizationtestsforlegacycodeGeneratingcharacterizationtestsforlegacycode
Generatingcharacterizationtestsforlegacycode
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Groovy Testing
Groovy TestingGroovy Testing
Groovy Testing
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testing
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
 
the next web now
the next web nowthe next web now
the next web now
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Annotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark ArtsAnnotation Processing - Demystifying Java's Dark Arts
Annotation Processing - Demystifying Java's Dark Arts
 
Building frameworks over Selenium
Building frameworks over SeleniumBuilding frameworks over Selenium
Building frameworks over Selenium
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
 
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
 
Android testing
Android testingAndroid testing
Android testing
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
 

Mehr von Jonas Follesø

Introduction to MonoTouch
Introduction to MonoTouchIntroduction to MonoTouch
Introduction to MonoTouch
Jonas Follesø
 

Mehr von Jonas Follesø (14)

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

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Generating characterization tests for legacy code

Hinweis der Redaktion

  1. Johannes BroadwallOle Christian RynningMarius B.KotsbakGeir Amdal