SlideShare a Scribd company logo
© Zühlke 2014
Rick Janda, Immo Hüneke
Less mocking, less brittle tests – BCS SPA 2014
Stop Mocking and Get Real
1. July 2014
Slide 1 of 41
© Zühlke 2014
Kai Zen
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 2 of 41
© Zühlke 2014
Stop Mocking and Get Real
Rick Janda, Immo Hüneke
What could be the problem
with Mock Objects?
1. July 2014
Slide 6 of 41
© Zühlke 2014
• …test the "How" and not the "What"
– Lead to fragile tests, thanks to multiple dependencies
• …mirror the code of the Unit under Test
– Violate the D.R.Y. principle
• …test based on unvalidated assumptions
– Foster a false sense of security
• … have complicated syntax to setup
– Make tests hard to read
Mock Objects…
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 7 of 41
© Zühlke 2014
Tell, don't ask
• Some commentators recommend the "tell, don't ask"
style of coding interactions to reduce the amount of
distributed state
• Where a number of collaborators are needed to
complete an interaction, each one typically returns
some value that is used as input to the next
• This leads to a proliferation of mock objects
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 8 of 41
© Zühlke 2014
• Pain Killers – alleviate the symptoms
• Treatments – address the real causes of pain
Escape from Mocking Hell
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 9 of 41
© Zühlke 2014
Stop Mocking and Get Real
Rick Janda, Immo Hüneke
- 3 Pain Killers -
1. July 2014
Slide 10 of 41
© Zühlke 2014
Pain Killer I: Use Stubs instead of Mocks
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 11 of 41
© Zühlke 2014
What does the test need to assert?
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 12 of 41
© Zühlke 2014
Use mocks only where needed (e.g. to test
collaboration or to inject return values)
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 13 of 41
© Zühlke 2014
Pain Killer II:
Give Unit Tests a single responsibility.
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 14 of 41
© Zühlke 2014
What can a unit test assert?
• Return value
• State of the UUT (Unit Under Test)
• State of a collaborator
• Behaviour of the UUT against a collaborator
Choose which of these your test is designed to prove.
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 15 of 41
© Zühlke 2014
Pain Killer III: Hand-code test doubles or
use library classes where possible
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 16 of 41
© Zühlke 2014
• ByteArrayInputStream and ByteArrayOutputStream for Streams
– Make assertions about the toString() result
– Allow changes in the how.
• CharArrayReader and CharArrayWriter for Reader/Writer
– Very similar
• MockHttpSession, MockHttpServletRequest and MockHttpServletResponse
for Servlet API interfaces
– part of Spring Testing
Examples
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 17 of 41
© Zühlke 2014
Stop Mocking and Get Real
Rick Janda, Immo Hüneke
Treatments that address
the causes of pain
Why do we use Mocks or Stubs in Unit Tests?
1. July 2014
Slide 18 of 41
© Zühlke 2014
Collaborators can be placed on an “awkwardness scale”
Test Doubles as Substitute Collaborators
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 19 of 41
|| | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | |
Simple and straightforward Hard AwkwardSoft Awkward
© Zühlke 2014
• Slow
• Hard to create
• Hard to control
Typical of physical resources or runtime environment:
• I/O
• Network
• Timer
• Stochastic
• Container
• Process
Awkward Collaborators
1. July 2014 Slide 20 of 41
© Zühlke 2014
• Fast
• Easy to create
• Easy to setup
• Easy access to state
Typical of more intelligent data structures:
• Value Objects or Transfer Objects
• Entities
• Composite data structures (Mapped XML or JSON)
• also: Method objects
Simple Collaborators
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 21 of 41
© Zühlke 2014
Simple Rule
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 22 of 41
© Zühlke 2014
- Design Changes -
Reducing the need for mocks and stubs
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 23 of 41
© Zühlke 2014
Tackle the causes of awkwardness
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 24 of 41
© Zühlke 2014
Isolation combats infection
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 25 of 41
© Zühlke 2014
Separation of concerns – an analogy
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 26 of 41
© Zühlke 2014
Categorise objects by their role
… refactoring where necessary
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 27 of 41
© Zühlke 2014
Refactoring for testability:
Separate concerns by subdividing methods
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 28 of 41
© Zühlke 2014
Refactoring for testability:
Separate concerns by redistributing code
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 29 of 41
© Zühlke 2014
Primary Constructor
• Eliminate need to inject an awkward collaborator by providing an
alternative constructor just used in testing
Pull out query to awkward collaborator
• Make query result an argument to a simple testable method
Separate decision making from decision execution
• Create a testable decision making method that returns selector or
argument values
• Use the result to invoke collaboration with awkward collaborator
Function below – objects above
• Organise functions under objects – simplify function arguments
Refactoring for testability:
Move the logic to the leaves
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 30 of 41
© Zühlke 2014
Extract Awkwardness into Adapters
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 31 of 41
© Zühlke 2014
Build Rich Domain Objects
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 32 of 41
© Zühlke 2014
After Separation into Awkward / Simple
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 33 of 41
© Zühlke 2014
Architecture for Testability:
Simple Core – Awkward Shell
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 34 of 41
© Zühlke 2014
Simple Core – Awkward Shell example
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 35 of 41
© Zühlke 2014
Architecture for Testability:
Simple Boundary
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 36 of 41
© Zühlke 2014
Simple Boundary Example:
Segregate the Awkward
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 37 of 41
© Zühlke 2014
Putting it All Together: Simple Core –
Awkward Shell & Simple Boundary
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 38 of 41
© Zühlke 2014
Your Tests Require Lots of Stubbing
and Mocking?
Change Your
Design
Towards
Better Testability!
Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 39 of 41
© Zühlke 2014
Change Your Design for Better Testability
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 40 of 41
© Zühlke 2014
Remember…
1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 41 of 41

More Related Content

Recently uploaded

The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
Sease
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
ScyllaDB
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
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
 

Recently uploaded (20)

The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
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
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Stop Mocking and Get Real

  • 1. © Zühlke 2014 Rick Janda, Immo Hüneke Less mocking, less brittle tests – BCS SPA 2014 Stop Mocking and Get Real 1. July 2014 Slide 1 of 41
  • 2. © Zühlke 2014 Kai Zen Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 2 of 41
  • 3.
  • 4.
  • 5.
  • 6. © Zühlke 2014 Stop Mocking and Get Real Rick Janda, Immo Hüneke What could be the problem with Mock Objects? 1. July 2014 Slide 6 of 41
  • 7. © Zühlke 2014 • …test the "How" and not the "What" – Lead to fragile tests, thanks to multiple dependencies • …mirror the code of the Unit under Test – Violate the D.R.Y. principle • …test based on unvalidated assumptions – Foster a false sense of security • … have complicated syntax to setup – Make tests hard to read Mock Objects… Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 7 of 41
  • 8. © Zühlke 2014 Tell, don't ask • Some commentators recommend the "tell, don't ask" style of coding interactions to reduce the amount of distributed state • Where a number of collaborators are needed to complete an interaction, each one typically returns some value that is used as input to the next • This leads to a proliferation of mock objects Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 8 of 41
  • 9. © Zühlke 2014 • Pain Killers – alleviate the symptoms • Treatments – address the real causes of pain Escape from Mocking Hell Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 9 of 41
  • 10. © Zühlke 2014 Stop Mocking and Get Real Rick Janda, Immo Hüneke - 3 Pain Killers - 1. July 2014 Slide 10 of 41
  • 11. © Zühlke 2014 Pain Killer I: Use Stubs instead of Mocks Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 11 of 41
  • 12. © Zühlke 2014 What does the test need to assert? 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 12 of 41
  • 13. © Zühlke 2014 Use mocks only where needed (e.g. to test collaboration or to inject return values) 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 13 of 41
  • 14. © Zühlke 2014 Pain Killer II: Give Unit Tests a single responsibility. Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 14 of 41
  • 15. © Zühlke 2014 What can a unit test assert? • Return value • State of the UUT (Unit Under Test) • State of a collaborator • Behaviour of the UUT against a collaborator Choose which of these your test is designed to prove. Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 15 of 41
  • 16. © Zühlke 2014 Pain Killer III: Hand-code test doubles or use library classes where possible Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 16 of 41
  • 17. © Zühlke 2014 • ByteArrayInputStream and ByteArrayOutputStream for Streams – Make assertions about the toString() result – Allow changes in the how. • CharArrayReader and CharArrayWriter for Reader/Writer – Very similar • MockHttpSession, MockHttpServletRequest and MockHttpServletResponse for Servlet API interfaces – part of Spring Testing Examples Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 17 of 41
  • 18. © Zühlke 2014 Stop Mocking and Get Real Rick Janda, Immo Hüneke Treatments that address the causes of pain Why do we use Mocks or Stubs in Unit Tests? 1. July 2014 Slide 18 of 41
  • 19. © Zühlke 2014 Collaborators can be placed on an “awkwardness scale” Test Doubles as Substitute Collaborators 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 19 of 41 || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | || | | | | Simple and straightforward Hard AwkwardSoft Awkward
  • 20. © Zühlke 2014 • Slow • Hard to create • Hard to control Typical of physical resources or runtime environment: • I/O • Network • Timer • Stochastic • Container • Process Awkward Collaborators 1. July 2014 Slide 20 of 41
  • 21. © Zühlke 2014 • Fast • Easy to create • Easy to setup • Easy access to state Typical of more intelligent data structures: • Value Objects or Transfer Objects • Entities • Composite data structures (Mapped XML or JSON) • also: Method objects Simple Collaborators 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 21 of 41
  • 22. © Zühlke 2014 Simple Rule 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 22 of 41
  • 23. © Zühlke 2014 - Design Changes - Reducing the need for mocks and stubs 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 23 of 41
  • 24. © Zühlke 2014 Tackle the causes of awkwardness 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 24 of 41
  • 25. © Zühlke 2014 Isolation combats infection 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 25 of 41
  • 26. © Zühlke 2014 Separation of concerns – an analogy 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 26 of 41
  • 27. © Zühlke 2014 Categorise objects by their role … refactoring where necessary 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 27 of 41
  • 28. © Zühlke 2014 Refactoring for testability: Separate concerns by subdividing methods 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 28 of 41
  • 29. © Zühlke 2014 Refactoring for testability: Separate concerns by redistributing code 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 29 of 41
  • 30. © Zühlke 2014 Primary Constructor • Eliminate need to inject an awkward collaborator by providing an alternative constructor just used in testing Pull out query to awkward collaborator • Make query result an argument to a simple testable method Separate decision making from decision execution • Create a testable decision making method that returns selector or argument values • Use the result to invoke collaboration with awkward collaborator Function below – objects above • Organise functions under objects – simplify function arguments Refactoring for testability: Move the logic to the leaves 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 30 of 41
  • 31. © Zühlke 2014 Extract Awkwardness into Adapters 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 31 of 41
  • 32. © Zühlke 2014 Build Rich Domain Objects 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 32 of 41
  • 33. © Zühlke 2014 After Separation into Awkward / Simple 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 33 of 41
  • 34. © Zühlke 2014 Architecture for Testability: Simple Core – Awkward Shell 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 34 of 41
  • 35. © Zühlke 2014 Simple Core – Awkward Shell example 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 35 of 41
  • 36. © Zühlke 2014 Architecture for Testability: Simple Boundary 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 36 of 41
  • 37. © Zühlke 2014 Simple Boundary Example: Segregate the Awkward 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 37 of 41
  • 38. © Zühlke 2014 Putting it All Together: Simple Core – Awkward Shell & Simple Boundary 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 38 of 41
  • 39. © Zühlke 2014 Your Tests Require Lots of Stubbing and Mocking? Change Your Design Towards Better Testability! Stop Mocking and Get Real | Rick Janda, Immo Hüneke 1. July 2014 Slide 39 of 41
  • 40. © Zühlke 2014 Change Your Design for Better Testability 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 40 of 41
  • 41. © Zühlke 2014 Remember… 1. July 2014Stop Mocking and Get Real | Rick Janda, Immo Hüneke Slide 41 of 41