SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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...
 

Mais de Jonas Follesø

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

Mais de 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
 

Último

Último (20)

A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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...
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Generating characterization tests for legacy code

Notas do Editor

  1. Johannes BroadwallOle Christian RynningMarius B.KotsbakGeir Amdal