SlideShare a Scribd company logo
Testable Code – Mind Shi2s
Testable Code – Mind Shi2s
Flaw: Constructor does Real Work
Flaw: Constructor does Real Work
• It violates the Single Responsibility Principle
• Tes7ng Directly is Difficult
• Subclassing and Overriding to Test is S7ll 
  Flawed
• It Forces Collaborators on You
Warning Signs
Warning Signs
• new keyword in a constructor or at field 
  declara<on 
• Sta<c method calls in a constructor or at field 
  declara<on 
• Anything more than field assignment in 
  constructors
• Adding or using an ini<aliza<on block
Warning Signs
Warning Signs
• Object not fully ini<alized a2er the constructor 
  finishes (watch out for ini<alize methods)
• Control flow (condi<onal or looping logic) in a 
  constructor
• CL does complex object graph construc<on 
  inside a constructor rather than using a factory 
  or builder 
Testability
Testability
Solu<on
 Do not create collaborators in your constructor, 
  but pass them in. (Don’t look for things! Ask 
  for things!)
Flaw: Digging into Collaborators
getUserManager().getUser(123).getProfile()
    .isAdmin() 
  // this is egregiously bad (all you need to know if 
    the user is an admin)

context.getCommonDataStore().find(1234)
  // this is bad
Warning Signs
Warning Signs
• Objects are passed in but never used directly 
  (only used to get access to other objects)
• Law of Demeter viola<on: method call chain 
  walks an object graph with more than one dot (.)
• Suspicious names: context, environment, 
  principal, container, or manager
Testability
Testability
Flaw: Bri9le Global State & Singletons
Flaw: Bri9le Global State & Singletons
• Spooky Ac<on at a Distance
Warning Signs
Warning Signs
•   Adding or using singletons 
•   Adding or using sta<c fields or sta<c methods 
•   Adding or using sta<c ini<aliza<on blocks 
•   Adding or using registries 
•   Adding or using service locators
Testability
Testability
Testability
Testability
Testability
Flaw: Class Does Too Much
Flaw: Class Does Too Much
a.k.a. …
• Kitchen Sink 
• Dumping Ground 
• Class who’s Behavior has too many “AND’s” 
• First thing’s KIll All The Managers (*See 
  Shakespeare) 
• God Class 
• “You can look at anything except for this one 
  class”
Warning Signs
Warning Signs
• Summing up what the class does includes the 
  word “and” 
• Class would be challenging for new team 
  members to read and quickly “get it” 
• Class has fields that are only used in some 
  methods 
• Class has sta<c methods that only operate on 
  parameters
Testability
It
Read

More Related Content

KEY
iOS Unit Testing
PDF
Testing without assertions - #HUSTEF2019
PPT
Unit Testing in iOS - Ninjava Talk
PDF
Hitchhiker's guide to Functional Testing
PPTX
TDD Basics with Angular.js and Jasmine
PPTX
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
PPT
Test-Driven SQL
PPTX
Magento code testability: Problems and Solutions
iOS Unit Testing
Testing without assertions - #HUSTEF2019
Unit Testing in iOS - Ninjava Talk
Hitchhiker's guide to Functional Testing
TDD Basics with Angular.js and Jasmine
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
Test-Driven SQL
Magento code testability: Problems and Solutions

What's hot (20)

PPTX
Anatomy of Test Driven Development
PPT
Unit Testing in iOS
PDF
iOS Test-Driven Development
PDF
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
PDF
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
PDF
Working Effectively with Legacy Code: Lessons in Practice
PPTX
VT.NET 20160411: An Intro to Test Driven Development (TDD)
PDF
Software Testing
PPTX
Dependency injection and Why It Matters to Testers
PPTX
Working with Legacy Code
PPTX
Journey to unit testing
PDF
How to apply AI to Testing
PDF
Unit Testing 101
PPTX
Understanding Unit Testing
PDF
Test Driven Development
PPTX
Battle of The Mocking Frameworks
PPT
Scrum and Test-driven development
PDF
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PDF
Testing Without Assertions
Anatomy of Test Driven Development
Unit Testing in iOS
iOS Test-Driven Development
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
Working Effectively with Legacy Code: Lessons in Practice
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Software Testing
Dependency injection and Why It Matters to Testers
Working with Legacy Code
Journey to unit testing
How to apply AI to Testing
Unit Testing 101
Understanding Unit Testing
Test Driven Development
Battle of The Mocking Frameworks
Scrum and Test-driven development
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Testing Without Assertions
Ad

Viewers also liked (19)

PDF
Our Space Time
PPTX
Continuous integration
PDF
blogging and you - a love story
PDF
Dabbawalas of Mumbai
PDF
What consultants can learn from Optimus Prime
PDF
Wild Australia - How safe are you?
PDF
The Consequences of Actions
PDF
The Chinese Zodiac
PPTX
Fighting Poverty in Urban America_Damon
PPTX
Einstein_PechaKucha_Tom
PDF
The Magic Never Ends
PDF
Canadian Winter Demystified
PDF
The Laws of Attraction
PPTX
My First Program - by Chris Reade
PDF
History of programming languages
PDF
What I learned from kids
PDF
Homebrewing
PDF
Soul Theory
PDF
Biometric Technology
Our Space Time
Continuous integration
blogging and you - a love story
Dabbawalas of Mumbai
What consultants can learn from Optimus Prime
Wild Australia - How safe are you?
The Consequences of Actions
The Chinese Zodiac
Fighting Poverty in Urban America_Damon
Einstein_PechaKucha_Tom
The Magic Never Ends
Canadian Winter Demystified
The Laws of Attraction
My First Program - by Chris Reade
History of programming languages
What I learned from kids
Homebrewing
Soul Theory
Biometric Technology
Ad

Similar to Testability (20)

PDF
Unit testing - 9 design hints
PPTX
Testing the Untestable
PDF
10 Principles of Apex Testing
PPTX
Clean code
PPTX
TDD Training
PPTX
Building unit tests correctly
PPTX
Code Testability
PPTX
10 Principles of Apex Testing
PPTX
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
PDF
33rd Degree 2013, Bad Tests, Good Tests
PPTX
Unit testing
PPTX
Mspec talk
PDF
How to Tame TDD - ISTA 2017
PPTX
Indy meetup#7 effective unit-testing-mule
PPTX
Principles and patterns for test driven development
PPTX
2016 10-04: tdd++: tdd made easier
PPTX
Software testing ... who’s responsible is it?
PPTX
Software testing ... who is responsible for it?
PDF
Developer Test - Things to Know
PPTX
White-box Unit Test Generation with Microsoft IntelliTest
Unit testing - 9 design hints
Testing the Untestable
10 Principles of Apex Testing
Clean code
TDD Training
Building unit tests correctly
Code Testability
10 Principles of Apex Testing
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
33rd Degree 2013, Bad Tests, Good Tests
Unit testing
Mspec talk
How to Tame TDD - ISTA 2017
Indy meetup#7 effective unit-testing-mule
Principles and patterns for test driven development
2016 10-04: tdd++: tdd made easier
Software testing ... who’s responsible is it?
Software testing ... who is responsible for it?
Developer Test - Things to Know
White-box Unit Test Generation with Microsoft IntelliTest

More from twuniversity (20)

PDF
Bollywood Dance Moves_Deepali
PPTX
Hindu Festivals of India_Apurva
PPTX
Deepawali_Saurabh
PPTX
Cars_PechaKucha_Ajith
PDF
Kite Surfing
PDF
Esoteric languages
PDF
Chinese people
PDF
Exploring the Sky
PDF
Bollywood
PDF
Hockey
PDF
Why collaborate
PDF
Entrepreneuralism
PDF
PDF
Aggressive inline skating
PDF
Humour
PDF
What have we learned from our mistakes?
PDF
The Family
PDF
Mr Bean
PDF
The truth about Tigers
PDF
Small Reflections of a Great House
Bollywood Dance Moves_Deepali
Hindu Festivals of India_Apurva
Deepawali_Saurabh
Cars_PechaKucha_Ajith
Kite Surfing
Esoteric languages
Chinese people
Exploring the Sky
Bollywood
Hockey
Why collaborate
Entrepreneuralism
Aggressive inline skating
Humour
What have we learned from our mistakes?
The Family
Mr Bean
The truth about Tigers
Small Reflections of a Great House

Recently uploaded (20)

PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PDF
Introduction to Generative Engine Optimization (GEO)
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Susan Semmelmann: Enriching the Lives of others through her Talents and Bless...
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PDF
Satish NS: Fostering Innovation and Sustainability: Haier India’s Customer-Ce...
PDF
Daniels 2024 Inclusive, Sustainable Development
PPTX
basic introduction to research chapter 1.pptx
PPTX
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
DOCX
Handbook of Entrepreneurship- Chapter 5: Identifying business opportunity.docx
PDF
NEW - FEES STRUCTURES (01-july-2024).pdf
PPTX
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
PPTX
interschool scomp.pptxzdkjhdjvdjvdjdhjhieij
PPTX
BUSINESS CYCLE_INFLATION AND UNEMPLOYMENT.pptx
PDF
Technical Architecture - Chainsys dataZap
PDF
TyAnn Osborn: A Visionary Leader Shaping Corporate Workforce Dynamics
PDF
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
PDF
Module 2 - Modern Supervison Challenges - Student Resource.pdf
PDF
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
PDF
Keppel_Proposed Divestment of M1 Limited
Slide gioi thieu VietinBank Quy 2 - 2025
Introduction to Generative Engine Optimization (GEO)
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Susan Semmelmann: Enriching the Lives of others through her Talents and Bless...
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Satish NS: Fostering Innovation and Sustainability: Haier India’s Customer-Ce...
Daniels 2024 Inclusive, Sustainable Development
basic introduction to research chapter 1.pptx
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
Handbook of Entrepreneurship- Chapter 5: Identifying business opportunity.docx
NEW - FEES STRUCTURES (01-july-2024).pdf
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
interschool scomp.pptxzdkjhdjvdjvdjdhjhieij
BUSINESS CYCLE_INFLATION AND UNEMPLOYMENT.pptx
Technical Architecture - Chainsys dataZap
TyAnn Osborn: A Visionary Leader Shaping Corporate Workforce Dynamics
Nante Industrial Plug Factory: Engineering Quality for Modern Power Applications
Module 2 - Modern Supervison Challenges - Student Resource.pdf
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
Keppel_Proposed Divestment of M1 Limited

Testability