SlideShare a Scribd company logo
SESSION DETAILS
• 10-15 MINUTES OF BACKGROUND • 50-55 MINUTES OF HANDS-ON CODING
SESSION DETAILS: YOU NEED THIS STUFF
• SEED CODE IS WRITTEN IN C#
• GET IT FROM GITHUB:
https://github.com/KatasForLegacy
Code/kCSharp/archive/Step0.zip
• OR FROM ONE OF THE FLASH DRIVES:
kCSharp-Step0.zip
• ANY VERSION OF VISUAL STUDIO 2012/2013
THAT CAN RUN CONSOLE APPS AND UNIT TESTS
• AN NUNIT TEST RUNNER OR THE ABILITY TO
QUICKLY SWITCH OUT NUNIT REFERENCE FOR
MSTEST. I RECOMMEND NCRUNCH.
• WHEN WE ARE DONE CODING, I’M HOPING WE
WILL HAVE 5-10 MINUTES FOR Q&A
• THE FIRST 5 SUGGESTIONS FOR ADDITIONS OR
IMPROVEMENTS TO THIS SESSION/CODE WILL
WIN THE FLASH DRIVES
LEGACY
EXPECTATION REALITY
LEGACY
PERCEPTION - COBOL
LEGACY
PERCEPTION - COBOL REALITY – CODE W/O UNIT TESTS
DEPENDENCY
EXPECTATION REALITY
DEPENDENCY – THE HORRID TRUTH
KILLER
EXPECTATION
“
”
I’VE COMPLETELY SLAUGHTERED ALL THE
LEGACY DEPENDENCIES IN OUR
CODEBASE.
NOW I’LL ENJOY SOME LUNCH.
KILLER
EXPECTATION REALITY
PUT IT ALL TOGETHER & WHADDAYA GET
•A CODE KATA
• A CODE KATA IS AN EXERCISE IN PROGRAMMING
WHICH HELPS A PROGRAMMER HONE THEIR
SKILLS THROUGH PRACTICE AND REPETITION
• THE WORD KATA IS TAKEN FROM JAPANESE ARTS
MOST TRADITIONALLY MARTIAL ARTS
• WHY? BECAUSE HUMANS LEARN BY DOING
DEPENDENCY KATA
• DEPENDENCY KATA: CODE CAMP EDITION
• WRITTEN IN C#
• NOW WITH LESS TOOL DEPENDENCIES AND RESTRUCTURED TO WORK WITHOUT THEM
• ORIGINAL SOURCE CODE AVAILABLE ON GITHUB AT HTTPS://GITHUB.COM/DUBMUN/DEPENDENCYKATA
DEPENDENCY KATA: INITIAL STRUCTURE
DEPENDENCY KATA: FIRST THINGS FIRST
• LET’S START BY GETTING THE EXISTING TEST
RUNNING
• RUNNING THE INTEGRATION TEST SHOWS THAT
IT HANGS
• WE NEED TO BEGIN BY ABSTRACTING AND
BREAKING THE DEPENDENCY ON
Console.Readline()
• CREATE AN INTERFACE FIRST
public interface IConsoleAdapter
{
string GetInput();
}
• CREATE A CLASS THAT IMPLEMENTS THE
INTERFACE
public class ConsoleAdapter :
IConsoleAdapter
DEPENDENCY KATA: FIRST THINGS FIRST
• IMPLEMENT METHOD TO HANDLE THE DEPENDENCY
public class ConsoleAdapter :
IConsoleAdapter
{
public string GetInput()
{
return Console.ReadLine();
}
}
• CREATE NEW CONSTRUCTOR FOR DOITALL THAT ACCEPTS
ICONSOLEADAPTER AND SET A PRIVATE VARIABLE
private IConsoleAdapter _consoleAdapter;
public doItAll(IConsoleAdapter
consoleAdapter)
{
_consoleAdapter = consoleAdapter;
}
• THEN REPLACE 6 CALLS TO Console.ReadLine()
WITH _consoleAdapter.GetInput()
DEPENDENCY KATA: FIRST THINGS FIRST
• NOW WE HAVE SOME BROKEN INSTANTIATIONS
• IN THE CONSOLE APP INSTANTIATE AND PASS IN
OUR NEW HANDLER
var doItAll = new DoItAll(
new ConsoleAdapter());
• IN THE INTEGRATION TEST WE NEED TO DO
SOMETHING DIFFERENT OR OUR TEST WILL STILL
FAIL.
• CREATE A NEW IMPLEMENTATION OF
IConsoleAdaper
public class FakeConsoleAdapter :
IConsoleAdapter
{
public string GetInput()
{
return string.Empty;
}
}
DEPENDENCY KATA: FIRST THINGS FIRST
• WHEN THE TEST IS RUN WE NOW GET A
MEANINGFUL EXCEPTION. ALL OF THE
DEPENDENCIES THAT CAUSED THE HANG ARE
GONE.
• THE TEST IS NOW FAILING BECAUSE THE
PASSWORD IS EMPTY. THIS IS A MEANINGFUL
CASE BUT LET’S JUST UPDATE OUR FAKE FOR
NOW.
return “fakeInput”;
• THE TEST SHOULD BE GREEN NOW!
DEPENDENCY KATA: FIRST THINGS FIRST
• WHEN THE TEST IS RUN WE NOW GET A
MEANINGFUL EXCEPTION. ALL OF THE
DEPENDENCIES THAT CAUSED THE HANG ARE
GONE.
• THE TEST IS NOW FAILING BECAUSE THE
PASSWORD IS EMPTY. THIS IS A MEANINGFUL
CASE BUT LET’S JUST UPDATE OUR FAKE FOR
NOW.
return “fakeInput”;
• THE TEST SHOULD BE GREEN NOW!
• LET’S ADD AN ASSERT FOR GOOD MEASURE
[Test, Category("Integration")]
public void DoItAll_Does_ItAll()
{
var doItAll = new DoItAll(new
FakeConsoleAdapter());
Assert.DoesNotThrow(() =>
doItAll.Do());
}
DEPENDENCY KATA: BETTER COVERAGE
• WE DON'T HAVE COVERAGE SOME OF THE CODE
STILL AND NO QUANTIFIABLE RESULTS TO TEST
• LET’S COPY OUR EXISTING TEST AND RENAME IT
DoItAll_Fails_ToWriteToDB
• THEN CHANGE THE ASSERT
StringAssert.Contains(
"Database.SaveToLog
Exception:", doItAll.Do());
• THIS WILL FAIL TO BUILD BECAUSE OUR METHOD IS
CURRENTLY VOID. LET’S CHANGE THAT
• CHANGE DO()’S RETURN TYPE TO STRING
• ADD A RETURN STATEMENT IN 2 LOCATIONS:
• AT THE END OF THE USING STATEMENT
• AT THE END OF THE METHOD
• NOW CREATE VARIABLES TO HOLD THE MESSAGES
AT VARIOUS POINTS FOR RETURN
private const string
_passwordsDontMatch =
"The passwords don't match.";
DEPENDENCY KATA: BETTER COVERAGE
• NOW CREATE VARIABLES TO HOLD THE MESSAGES
AT VARIOUS POINTS FOR RETURN
private const string
_passwordsDontMatch =
"The passwords don't match.";
AND
var errorMessage = string.Format(
"{0} - Database.SaveToLog
Exception: rn{1}",
message, ex.Message);
• RETURN THOSE VALUES AS APPROPRIATE
• OUR NEW TEST SHOULD NOW PASS
DEPENDENCY KATA: BETTER ABSTRACTION
• DO WORK TO ABSTRACT CONSOLE COMPLETELY
COMPLETELY
• ADD A NEW METHOD STUB TO
IConsoleAdapter:
Void SetOutput(string output);
• UPDATE ConsoleAdapter IMPLEMENTATION
public void SetOutput(string output)
{
Console.WriteLine(output);
}
• UPDATE FakeConsoleAdapter
IMPLEMENTATION
public void SetOutput(string
output)
{}
• UPDATE DoItAll IMPLEMENTATION REPLACING
6 INSTANCES OF Console.WriteLine WITH
_consoleAdapter.SetOutput
• OUR TESTS SHOULD STILL PASS
DEPENDENCY KATA: REFACTOR
• DoItAll.Do() IS TRYING TO DO TOO MUCH
• EXTRACT LOGGING FUNCTIONALITY BY CREATING
A NEW INTERFACE
public interface ILogger
{
string LogMessage(string message);
}
• NOW EXTRACT THE CODE IN THE TRY/CATCH
BLOCK IN DoItAll.Do() INTO THE
IMPLEMENTATION OF
ILogger.LogMessage()
• MAKE SURE ALL PATHS RETURN A MESSAGE
• NOW UPDATE THE CONSTRUCTOR OF DoItAll
WITH AN ILogger SAND REPLACE TRY/CATCH:
message =
_logger.LogMessage(message);
DEPENDENCY KATA: REFACTOR
• CLIENT NO LONGER BUILDS BECAUSE OF THE
UPDATED CONSTRUCTOR SO UPDATE IT
• NOW CREATE A NEW FAKE FOR TESTING:
public class FakeLogger : ILogger
{
public string LogMessage(
string message)
{
return string.Empty;
}
}
• UPDATE THE DoItAll MINSTANTIATIONS IN
THE TESTS TO INCLUDE new FakeLogger()
• THE FIRST TEST PASSES BUT THE OTHER FAILS
BECAUSE IT DEPENDS ON IMPLEMENTATION-
SPECIFIC DETAILS
DEPENDENCY KATA: REFACTOR
• COPY THE SECOND TEST AND RENAME THE COPY
DoItAll_Succeeds_WithMockLogging
• UPDATE THE ASSERT:
Assert.AreEqual(
string.Empty, doItAll.Do());
• TEST WILL PASS
• LET THE FORMER TEST DEPEND ON
DatabaseLogger AND IT WILL PASS AS WELL
DEPENDENCY KATA: WHAT’S NEXT?
• THERE ARE STILL PLENTY OF THING ABOUT THIS LEGACY CODE I WOULD CHANGE
• WHAT WOULD YOU DO NEXT?
THANKS TO OUR SPONSORS!
To connect to wireless
1. Choose Uguest in the wireless list
2. Open a browser. This will open a Uof U website
3. Choose Login
Legacy Dependency Killer - Utah Code Camp 2014

More Related Content

What's hot

Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
Mark Wragg
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
Andrea Francia
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
intuit_india
 
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
DevOpsGroup
 
Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)
Faysal Hossain Shezan
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce Apps
Daniel Stange
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
VodqaBLR
 
Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8
Allen Pike
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from API
Roman Liubun
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?
Bertrand Delacretaz
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
Roman Liubun
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Nascenia IT
 
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Icinga
 
Learnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHPLearnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHP
Ralf Eggert
 
Axon Server went RAFTing
Axon Server went RAFTingAxon Server went RAFTing
Axon Server went RAFTing
J On The Beach
 
Testing in Ballerina Language
Testing in Ballerina LanguageTesting in Ballerina Language
Testing in Ballerina Language
Lynn Langit
 
Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012
txels
 
WordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version ControlWordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version Control
Sterling Hamilton
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
Kerry Buckley
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver Specification
Peter Thomas
 

What's hot (20)

Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
 
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
 
Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce Apps
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
 
Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from API
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
 
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
 
Learnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHPLearnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHP
 
Axon Server went RAFTing
Axon Server went RAFTingAxon Server went RAFTing
Axon Server went RAFTing
 
Testing in Ballerina Language
Testing in Ballerina LanguageTesting in Ballerina Language
Testing in Ballerina Language
 
Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012
 
WordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version ControlWordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version Control
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver Specification
 

Similar to Legacy Dependency Killer - Utah Code Camp 2014

Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
Eric Phan
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
2013-01-10 iOS testing
2013-01-10 iOS testing2013-01-10 iOS testing
2013-01-10 iOS testing
CocoaHeads Tricity
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
Giordano Scalzo
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
Doommaker
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
Hendrik Ebbers
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
Martin Gutenbrunner
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
Alina Vilk
 
iOS testing
iOS testingiOS testing
iOS testing
Tomasz Janeczko
 
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaDsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Ugo Matrangolo
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
Peter Kofler
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your tests
Roy van Rijn
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van Rijn
NLJUG
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
Andy Butland
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
Ortus Solutions, Corp
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
Chhom Karath
 

Similar to Legacy Dependency Killer - Utah Code Camp 2014 (20)

Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
2013-01-10 iOS testing
2013-01-10 iOS testing2013-01-10 iOS testing
2013-01-10 iOS testing
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
 
iOS testing
iOS testingiOS testing
iOS testing
 
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaDsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your tests
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van Rijn
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
 

Recently uploaded

WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
Data Hops
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
maazsz111
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 

Recently uploaded (20)

WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 

Legacy Dependency Killer - Utah Code Camp 2014

  • 1.
  • 2. SESSION DETAILS • 10-15 MINUTES OF BACKGROUND • 50-55 MINUTES OF HANDS-ON CODING
  • 3. SESSION DETAILS: YOU NEED THIS STUFF • SEED CODE IS WRITTEN IN C# • GET IT FROM GITHUB: https://github.com/KatasForLegacy Code/kCSharp/archive/Step0.zip • OR FROM ONE OF THE FLASH DRIVES: kCSharp-Step0.zip • ANY VERSION OF VISUAL STUDIO 2012/2013 THAT CAN RUN CONSOLE APPS AND UNIT TESTS • AN NUNIT TEST RUNNER OR THE ABILITY TO QUICKLY SWITCH OUT NUNIT REFERENCE FOR MSTEST. I RECOMMEND NCRUNCH. • WHEN WE ARE DONE CODING, I’M HOPING WE WILL HAVE 5-10 MINUTES FOR Q&A • THE FIRST 5 SUGGESTIONS FOR ADDITIONS OR IMPROVEMENTS TO THIS SESSION/CODE WILL WIN THE FLASH DRIVES
  • 6. LEGACY PERCEPTION - COBOL REALITY – CODE W/O UNIT TESTS
  • 8. DEPENDENCY – THE HORRID TRUTH
  • 10. “ ” I’VE COMPLETELY SLAUGHTERED ALL THE LEGACY DEPENDENCIES IN OUR CODEBASE. NOW I’LL ENJOY SOME LUNCH.
  • 12. PUT IT ALL TOGETHER & WHADDAYA GET •A CODE KATA • A CODE KATA IS AN EXERCISE IN PROGRAMMING WHICH HELPS A PROGRAMMER HONE THEIR SKILLS THROUGH PRACTICE AND REPETITION • THE WORD KATA IS TAKEN FROM JAPANESE ARTS MOST TRADITIONALLY MARTIAL ARTS • WHY? BECAUSE HUMANS LEARN BY DOING
  • 13. DEPENDENCY KATA • DEPENDENCY KATA: CODE CAMP EDITION • WRITTEN IN C# • NOW WITH LESS TOOL DEPENDENCIES AND RESTRUCTURED TO WORK WITHOUT THEM • ORIGINAL SOURCE CODE AVAILABLE ON GITHUB AT HTTPS://GITHUB.COM/DUBMUN/DEPENDENCYKATA
  • 15. DEPENDENCY KATA: FIRST THINGS FIRST • LET’S START BY GETTING THE EXISTING TEST RUNNING • RUNNING THE INTEGRATION TEST SHOWS THAT IT HANGS • WE NEED TO BEGIN BY ABSTRACTING AND BREAKING THE DEPENDENCY ON Console.Readline() • CREATE AN INTERFACE FIRST public interface IConsoleAdapter { string GetInput(); } • CREATE A CLASS THAT IMPLEMENTS THE INTERFACE public class ConsoleAdapter : IConsoleAdapter
  • 16. DEPENDENCY KATA: FIRST THINGS FIRST • IMPLEMENT METHOD TO HANDLE THE DEPENDENCY public class ConsoleAdapter : IConsoleAdapter { public string GetInput() { return Console.ReadLine(); } } • CREATE NEW CONSTRUCTOR FOR DOITALL THAT ACCEPTS ICONSOLEADAPTER AND SET A PRIVATE VARIABLE private IConsoleAdapter _consoleAdapter; public doItAll(IConsoleAdapter consoleAdapter) { _consoleAdapter = consoleAdapter; } • THEN REPLACE 6 CALLS TO Console.ReadLine() WITH _consoleAdapter.GetInput()
  • 17. DEPENDENCY KATA: FIRST THINGS FIRST • NOW WE HAVE SOME BROKEN INSTANTIATIONS • IN THE CONSOLE APP INSTANTIATE AND PASS IN OUR NEW HANDLER var doItAll = new DoItAll( new ConsoleAdapter()); • IN THE INTEGRATION TEST WE NEED TO DO SOMETHING DIFFERENT OR OUR TEST WILL STILL FAIL. • CREATE A NEW IMPLEMENTATION OF IConsoleAdaper public class FakeConsoleAdapter : IConsoleAdapter { public string GetInput() { return string.Empty; } }
  • 18. DEPENDENCY KATA: FIRST THINGS FIRST • WHEN THE TEST IS RUN WE NOW GET A MEANINGFUL EXCEPTION. ALL OF THE DEPENDENCIES THAT CAUSED THE HANG ARE GONE. • THE TEST IS NOW FAILING BECAUSE THE PASSWORD IS EMPTY. THIS IS A MEANINGFUL CASE BUT LET’S JUST UPDATE OUR FAKE FOR NOW. return “fakeInput”; • THE TEST SHOULD BE GREEN NOW!
  • 19. DEPENDENCY KATA: FIRST THINGS FIRST • WHEN THE TEST IS RUN WE NOW GET A MEANINGFUL EXCEPTION. ALL OF THE DEPENDENCIES THAT CAUSED THE HANG ARE GONE. • THE TEST IS NOW FAILING BECAUSE THE PASSWORD IS EMPTY. THIS IS A MEANINGFUL CASE BUT LET’S JUST UPDATE OUR FAKE FOR NOW. return “fakeInput”; • THE TEST SHOULD BE GREEN NOW! • LET’S ADD AN ASSERT FOR GOOD MEASURE [Test, Category("Integration")] public void DoItAll_Does_ItAll() { var doItAll = new DoItAll(new FakeConsoleAdapter()); Assert.DoesNotThrow(() => doItAll.Do()); }
  • 20. DEPENDENCY KATA: BETTER COVERAGE • WE DON'T HAVE COVERAGE SOME OF THE CODE STILL AND NO QUANTIFIABLE RESULTS TO TEST • LET’S COPY OUR EXISTING TEST AND RENAME IT DoItAll_Fails_ToWriteToDB • THEN CHANGE THE ASSERT StringAssert.Contains( "Database.SaveToLog Exception:", doItAll.Do()); • THIS WILL FAIL TO BUILD BECAUSE OUR METHOD IS CURRENTLY VOID. LET’S CHANGE THAT • CHANGE DO()’S RETURN TYPE TO STRING • ADD A RETURN STATEMENT IN 2 LOCATIONS: • AT THE END OF THE USING STATEMENT • AT THE END OF THE METHOD • NOW CREATE VARIABLES TO HOLD THE MESSAGES AT VARIOUS POINTS FOR RETURN private const string _passwordsDontMatch = "The passwords don't match.";
  • 21. DEPENDENCY KATA: BETTER COVERAGE • NOW CREATE VARIABLES TO HOLD THE MESSAGES AT VARIOUS POINTS FOR RETURN private const string _passwordsDontMatch = "The passwords don't match."; AND var errorMessage = string.Format( "{0} - Database.SaveToLog Exception: rn{1}", message, ex.Message); • RETURN THOSE VALUES AS APPROPRIATE • OUR NEW TEST SHOULD NOW PASS
  • 22. DEPENDENCY KATA: BETTER ABSTRACTION • DO WORK TO ABSTRACT CONSOLE COMPLETELY COMPLETELY • ADD A NEW METHOD STUB TO IConsoleAdapter: Void SetOutput(string output); • UPDATE ConsoleAdapter IMPLEMENTATION public void SetOutput(string output) { Console.WriteLine(output); } • UPDATE FakeConsoleAdapter IMPLEMENTATION public void SetOutput(string output) {} • UPDATE DoItAll IMPLEMENTATION REPLACING 6 INSTANCES OF Console.WriteLine WITH _consoleAdapter.SetOutput • OUR TESTS SHOULD STILL PASS
  • 23. DEPENDENCY KATA: REFACTOR • DoItAll.Do() IS TRYING TO DO TOO MUCH • EXTRACT LOGGING FUNCTIONALITY BY CREATING A NEW INTERFACE public interface ILogger { string LogMessage(string message); } • NOW EXTRACT THE CODE IN THE TRY/CATCH BLOCK IN DoItAll.Do() INTO THE IMPLEMENTATION OF ILogger.LogMessage() • MAKE SURE ALL PATHS RETURN A MESSAGE • NOW UPDATE THE CONSTRUCTOR OF DoItAll WITH AN ILogger SAND REPLACE TRY/CATCH: message = _logger.LogMessage(message);
  • 24. DEPENDENCY KATA: REFACTOR • CLIENT NO LONGER BUILDS BECAUSE OF THE UPDATED CONSTRUCTOR SO UPDATE IT • NOW CREATE A NEW FAKE FOR TESTING: public class FakeLogger : ILogger { public string LogMessage( string message) { return string.Empty; } } • UPDATE THE DoItAll MINSTANTIATIONS IN THE TESTS TO INCLUDE new FakeLogger() • THE FIRST TEST PASSES BUT THE OTHER FAILS BECAUSE IT DEPENDS ON IMPLEMENTATION- SPECIFIC DETAILS
  • 25. DEPENDENCY KATA: REFACTOR • COPY THE SECOND TEST AND RENAME THE COPY DoItAll_Succeeds_WithMockLogging • UPDATE THE ASSERT: Assert.AreEqual( string.Empty, doItAll.Do()); • TEST WILL PASS • LET THE FORMER TEST DEPEND ON DatabaseLogger AND IT WILL PASS AS WELL
  • 26. DEPENDENCY KATA: WHAT’S NEXT? • THERE ARE STILL PLENTY OF THING ABOUT THIS LEGACY CODE I WOULD CHANGE • WHAT WOULD YOU DO NEXT?
  • 27. THANKS TO OUR SPONSORS! To connect to wireless 1. Choose Uguest in the wireless list 2. Open a browser. This will open a Uof U website 3. Choose Login