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

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

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