SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Downloaden Sie, um offline zu lesen
© 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

Weitere ähnliche Inhalte

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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 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...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Empfohlen

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)
 

Empfohlen (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