SlideShare a Scribd company logo
1 of 16
Test Doubles Mock Objects & Design Principles SWE-795, Test Driven Development, GMU Spring 2011 – Bill Shelton
“Once”, the Mock Turtle said at last, with a deep sigh, “I was a real turtle.” Lewis Carroll, Alice’s Adventure in Wonderland
Types of Test Doubles Gerard Meszaros, xUnit Test Patterns, http://xunitpatterns.com/Test%20Double%20Patterns.html Fakes, Stubs, Mocks, Spies, Dummies, etc. Fowler draws lines between mocks and stubs, yet “mocking” tools do both. ,[object Object],[object Object]
Dependency Example code: https://gist.github.com/840494
Ways to build Mocks Code by hand (Fakes) Extend the depended-upon object and override methods Build lightweight representations; e.g., anonymous classes What does this smell like? On-the-fly with a mocking framework Lots to choose from Almost all have some limitations
State and Behavior Verification State-based tests assert the resultant state of an operation on the object under test Behavior-based tests verifyhow the object under test interacts with its collaborators Should these be considered mutually exclusive activities?
<code/> : https://gist.github.com/840494
Classicists and Mockists(Fowler)  Coupling to Implementation: Classicists think it’s important to only think about the external interface, whereas Mockistsmust consider the implementation … What about considering various paths in state-based tests?Classicist do need to look beyond the interface. This is a design activity. Test Isolation: Fowler seems to imply that this is less important to state-based tests  * Test Driven Design Idioms (Let’s see some case studies) Outside-in vs. Middle-out “Tell Don’t Ask” Law of Demeter, or nearest neighbor principle
Discovering New Types Freeman and Pryce describe Need-Driven Development as an outside-in approach Thinking in terms of layers, starting with the outside first, build inwards using Mock Objects to discover interface needs An interesting Agile idea, but in practice how does it work? Case study? Process: Refer to earlier Dependency Slide Collaborators evolve and become SUT
Summary Mocks can be used for: Managing test-time dependencies (databases, network services, email, etc.) Verifying howthe object under test interacts with its collaborators Driving an outside-in design process Discovery of new types Other points Decouple dependencies in code to allow injection of mocks; e.g.,  setCollaborator(Collaborator c) Consider control and isloation
Other Applications for Mocks? It appears that Mocks have only been considered as an aid to testing, design, and TDD.  If Mocks can simulate behavior, could they be used for other purposes? Software simulation Simulate faulty behavior or state … Mutant behavior Given a known behavior of the application of a specific mutant operator, could we simulate that using Mock Objects?
Some mocking frameworks Java jMock, EasyMock, PowerMock, jMockit, Mockito .NET NMock, moq, EasyMock.NET, Rhino Mocks Python Pymox, dingus  ColdFusion MightyMock (part of MXUnit), MockBox, ColdMock, CFEasyMock Ruby rr, mocha, flexmock, stump, facon
Geeky Chuck Norris Jokes Chuck Norris can divide by Zero. Chuck Norris can count to infinity … twice! Chuck Norris doesn’t do TDD. Bugs are too damn scared to go anywhere near his code.
Complete Path Coverage CPC Prime Path Coverage PPC Edge-Pair Coverage EPC All-DU-Paths Coverage ADUP All-uses Coverage AUC All-defs Coverage ADC Edge Coverage EC Simple Round Trip Coverage SRTC Complete Round Trip Coverage CRTC Node Coverage NC Introduction to Software Testing  (Ch 2) © Ammann & Offutt
Complete Path Coverage CPC Prime Path Coverage PPC Edge-Pair Coverage EPC All-DU-Paths Coverage ADUP All-uses Coverage AUC All-defs Coverage ADC Edge Coverage EC Simple Round Trip Coverage SRTC Complete Round Trip Coverage CRTC Node Coverage NC The Chuck Norris Criterion  subsumes ALL other criteria. Period. The End. Chuck NorrisCoverageCNC Introduction to Software Testing  (Ch 2) © Ammann & Offutt
Test Doubles

More Related Content

Similar to Test Doubles

Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testingmarkstory
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking FrameworksDror Helper
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NETPuneet Ghanshani
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Introduction of abm
Introduction of abmIntroduction of abm
Introduction of abmyudiyasik
 
Introduction of abm
Introduction of abmIntroduction of abm
Introduction of abmyudiyasik
 
10 Ways To Improve Your Code( Neal Ford)
10  Ways To  Improve  Your  Code( Neal  Ford)10  Ways To  Improve  Your  Code( Neal  Ford)
10 Ways To Improve Your Code( Neal Ford)guestebde
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Grzegorz Miejski
 
Chaos Engineering - The Art of Breaking Things in Production
Chaos Engineering - The Art of Breaking Things in ProductionChaos Engineering - The Art of Breaking Things in Production
Chaos Engineering - The Art of Breaking Things in ProductionKeet Sugathadasa
 
A Tale of Two Patterns
A Tale of Two PatternsA Tale of Two Patterns
A Tale of Two PatternsKevlin Henney
 

Similar to Test Doubles (20)

Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testing
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking Frameworks
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Complexity
ComplexityComplexity
Complexity
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Introduction of abm
Introduction of abmIntroduction of abm
Introduction of abm
 
Introduction of abm
Introduction of abmIntroduction of abm
Introduction of abm
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
10 Ways To Improve Your Code( Neal Ford)
10  Ways To  Improve  Your  Code( Neal  Ford)10  Ways To  Improve  Your  Code( Neal  Ford)
10 Ways To Improve Your Code( Neal Ford)
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019
 
Chaos Engineering - The Art of Breaking Things in Production
Chaos Engineering - The Art of Breaking Things in ProductionChaos Engineering - The Art of Breaking Things in Production
Chaos Engineering - The Art of Breaking Things in Production
 
A Tale of Two Patterns
A Tale of Two PatternsA Tale of Two Patterns
A Tale of Two Patterns
 

Recently uploaded

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...Martijn de Jong
 
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 Subbuapidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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 FresherRemote DBA Services
 
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 WoodJuan lago vázquez
 
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.pdfsudhanshuwaghmare1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
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...DianaGray10
 
"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 ...Zilliz
 
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?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Recently uploaded (20)

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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
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
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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...
 
"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 ...
 
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?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Test Doubles

  • 1. Test Doubles Mock Objects & Design Principles SWE-795, Test Driven Development, GMU Spring 2011 – Bill Shelton
  • 2. “Once”, the Mock Turtle said at last, with a deep sigh, “I was a real turtle.” Lewis Carroll, Alice’s Adventure in Wonderland
  • 3.
  • 4. Dependency Example code: https://gist.github.com/840494
  • 5. Ways to build Mocks Code by hand (Fakes) Extend the depended-upon object and override methods Build lightweight representations; e.g., anonymous classes What does this smell like? On-the-fly with a mocking framework Lots to choose from Almost all have some limitations
  • 6. State and Behavior Verification State-based tests assert the resultant state of an operation on the object under test Behavior-based tests verifyhow the object under test interacts with its collaborators Should these be considered mutually exclusive activities?
  • 8. Classicists and Mockists(Fowler) Coupling to Implementation: Classicists think it’s important to only think about the external interface, whereas Mockistsmust consider the implementation … What about considering various paths in state-based tests?Classicist do need to look beyond the interface. This is a design activity. Test Isolation: Fowler seems to imply that this is less important to state-based tests * Test Driven Design Idioms (Let’s see some case studies) Outside-in vs. Middle-out “Tell Don’t Ask” Law of Demeter, or nearest neighbor principle
  • 9. Discovering New Types Freeman and Pryce describe Need-Driven Development as an outside-in approach Thinking in terms of layers, starting with the outside first, build inwards using Mock Objects to discover interface needs An interesting Agile idea, but in practice how does it work? Case study? Process: Refer to earlier Dependency Slide Collaborators evolve and become SUT
  • 10. Summary Mocks can be used for: Managing test-time dependencies (databases, network services, email, etc.) Verifying howthe object under test interacts with its collaborators Driving an outside-in design process Discovery of new types Other points Decouple dependencies in code to allow injection of mocks; e.g., setCollaborator(Collaborator c) Consider control and isloation
  • 11. Other Applications for Mocks? It appears that Mocks have only been considered as an aid to testing, design, and TDD. If Mocks can simulate behavior, could they be used for other purposes? Software simulation Simulate faulty behavior or state … Mutant behavior Given a known behavior of the application of a specific mutant operator, could we simulate that using Mock Objects?
  • 12. Some mocking frameworks Java jMock, EasyMock, PowerMock, jMockit, Mockito .NET NMock, moq, EasyMock.NET, Rhino Mocks Python Pymox, dingus ColdFusion MightyMock (part of MXUnit), MockBox, ColdMock, CFEasyMock Ruby rr, mocha, flexmock, stump, facon
  • 13. Geeky Chuck Norris Jokes Chuck Norris can divide by Zero. Chuck Norris can count to infinity … twice! Chuck Norris doesn’t do TDD. Bugs are too damn scared to go anywhere near his code.
  • 14. Complete Path Coverage CPC Prime Path Coverage PPC Edge-Pair Coverage EPC All-DU-Paths Coverage ADUP All-uses Coverage AUC All-defs Coverage ADC Edge Coverage EC Simple Round Trip Coverage SRTC Complete Round Trip Coverage CRTC Node Coverage NC Introduction to Software Testing (Ch 2) © Ammann & Offutt
  • 15. Complete Path Coverage CPC Prime Path Coverage PPC Edge-Pair Coverage EPC All-DU-Paths Coverage ADUP All-uses Coverage AUC All-defs Coverage ADC Edge Coverage EC Simple Round Trip Coverage SRTC Complete Round Trip Coverage CRTC Node Coverage NC The Chuck Norris Criterion subsumes ALL other criteria. Period. The End. Chuck NorrisCoverageCNC Introduction to Software Testing (Ch 2) © Ammann & Offutt

Editor's Notes

  1. What are they?
  2. What are test doubles?
  3. Example dependencies: Databases, File I/O, network services, email …Show what dependecies look like, and how a simple design change can make code more testable (Dependency Injection vs. Instrumentation)
  4. Show Emailer.java and BetterEmailer.java