SlideShare a Scribd company logo
1 of 31
Download to read offline
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 
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
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
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
It
Read

More Related Content

What's hot

Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven development
toteb5
 
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
FAQ - why does my code throw a null pointer exception - common reason #1 Rede...
Alan Richardson
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove
 

What's hot (20)

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

Viewers also liked

Continuous integration
Continuous integrationContinuous integration
Continuous integration
twuniversity
 
Fighting Poverty in Urban America_Damon
Fighting Poverty in Urban America_DamonFighting Poverty in Urban America_Damon
Fighting Poverty in Urban America_Damon
twuniversity
 
Einstein_PechaKucha_Tom
Einstein_PechaKucha_TomEinstein_PechaKucha_Tom
Einstein_PechaKucha_Tom
twuniversity
 

Viewers also liked (19)

Our Space Time
Our Space TimeOur Space Time
Our Space Time
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
blogging and you - a love story
blogging and you - a love storyblogging and you - a love story
blogging and you - a love story
 
Dabbawalas of Mumbai
Dabbawalas of MumbaiDabbawalas of Mumbai
Dabbawalas of Mumbai
 
What consultants can learn from Optimus Prime
What consultants can learn from Optimus PrimeWhat consultants can learn from Optimus Prime
What consultants can learn from Optimus Prime
 
Wild Australia - How safe are you?
Wild Australia - How safe are you?Wild Australia - How safe are you?
Wild Australia - How safe are you?
 
The Consequences of Actions
The Consequences of ActionsThe Consequences of Actions
The Consequences of Actions
 
The Chinese Zodiac
The Chinese ZodiacThe Chinese Zodiac
The Chinese Zodiac
 
Fighting Poverty in Urban America_Damon
Fighting Poverty in Urban America_DamonFighting Poverty in Urban America_Damon
Fighting Poverty in Urban America_Damon
 
Einstein_PechaKucha_Tom
Einstein_PechaKucha_TomEinstein_PechaKucha_Tom
Einstein_PechaKucha_Tom
 
The Magic Never Ends
The Magic Never EndsThe Magic Never Ends
The Magic Never Ends
 
Canadian Winter Demystified
Canadian Winter DemystifiedCanadian Winter Demystified
Canadian Winter Demystified
 
The Laws of Attraction
The Laws of AttractionThe Laws of Attraction
The Laws of Attraction
 
My First Program - by Chris Reade
My First Program - by Chris ReadeMy First Program - by Chris Reade
My First Program - by Chris Reade
 
History of programming languages
History of programming languagesHistory of programming languages
History of programming languages
 
What I learned from kids
What I learned from kidsWhat I learned from kids
What I learned from kids
 
Homebrewing
HomebrewingHomebrewing
Homebrewing
 
Soul Theory
Soul TheorySoul Theory
Soul Theory
 
Biometric Technology
Biometric TechnologyBiometric Technology
Biometric Technology
 

Similar to Testability

Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true story
Aki Salmi
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
P Heinonen
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
UA Mobile
 

Similar to Testability (20)

Write testable code in java, best practices
Write testable code in java, best practicesWrite testable code in java, best practices
Write testable code in java, best practices
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiences
 
Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true story
 
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing in Swift
Unit Testing in SwiftUnit Testing in Swift
Unit Testing in Swift
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Refactoring workshop
Refactoring workshop Refactoring workshop
Refactoring workshop
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
Software testing ... who’s responsible is it?
Software testing ... who’s responsible is it?Software testing ... who’s responsible is it?
Software testing ... who’s responsible is it?
 
Software testing ... who is responsible for it?
Software testing ... who is responsible for it?Software testing ... who is responsible for it?
Software testing ... who is responsible for it?
 
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016
 
Devops Journey - internet tech startup
Devops Journey - internet tech startupDevops Journey - internet tech startup
Devops Journey - internet tech startup
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Implementing Quality on a Java Project
Implementing Quality on a Java ProjectImplementing Quality on a Java Project
Implementing Quality on a Java Project
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 

More from twuniversity (20)

Bollywood Dance Moves_Deepali
Bollywood Dance Moves_DeepaliBollywood Dance Moves_Deepali
Bollywood Dance Moves_Deepali
 
Hindu Festivals of India_Apurva
Hindu Festivals of India_ApurvaHindu Festivals of India_Apurva
Hindu Festivals of India_Apurva
 
Deepawali_Saurabh
Deepawali_SaurabhDeepawali_Saurabh
Deepawali_Saurabh
 
Cars_PechaKucha_Ajith
Cars_PechaKucha_AjithCars_PechaKucha_Ajith
Cars_PechaKucha_Ajith
 
Kite Surfing
Kite SurfingKite Surfing
Kite Surfing
 
Esoteric languages
Esoteric languagesEsoteric languages
Esoteric languages
 
Chinese people
Chinese peopleChinese people
Chinese people
 
Exploring the Sky
Exploring the SkyExploring the Sky
Exploring the Sky
 
Bollywood
BollywoodBollywood
Bollywood
 
Hockey
HockeyHockey
Hockey
 
Why collaborate
Why collaborateWhy collaborate
Why collaborate
 
Entrepreneuralism
EntrepreneuralismEntrepreneuralism
Entrepreneuralism
 
Earth
EarthEarth
Earth
 
Aggressive inline skating
Aggressive inline skatingAggressive inline skating
Aggressive inline skating
 
Humour
HumourHumour
Humour
 
What have we learned from our mistakes?
What have we learned from our mistakes?What have we learned from our mistakes?
What have we learned from our mistakes?
 
The Family
The FamilyThe Family
The Family
 
Mr Bean
Mr BeanMr Bean
Mr Bean
 
The truth about Tigers
The truth about TigersThe truth about Tigers
The truth about Tigers
 
Small Reflections of a Great House
Small Reflections of a Great HouseSmall Reflections of a Great House
Small Reflections of a Great House
 

Recently uploaded

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
Renandantas16
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
lizamodels9
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
Matteo Carbone
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
amitlee9823
 

Recently uploaded (20)

Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Greater Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
John Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdfJohn Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdf
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 

Testability