SlideShare uma empresa Scribd logo
1 de 130
Testing and Testable code Paweł Szulc http://paulszulc.wordpress.com [email_address]
Testing and testable code ,[object Object],[object Object],[object Object]
Java Developer (currently Wicket+Spring+JPA)
Agile Enthusiast ,[object Object]
E-mail: paul.szulc@gmail.com
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object]
Testing
Testing and testable code ,[object Object],[object Object]
Testing
Testable code
Testing and testable code ,[object Object],[object Object]
Testing
Testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Testing
Testable code ,[object Object],DESIGN
Testing and testable code ,[object Object],[object Object]
Testing
Testable code ,[object Object],DESIGN controversial?
Testing and testable code ,[object Object]
Testing and testable code ,[object Object]
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object]
After-party conclusion  ,[object Object]
I don't use TDD because ,[object Object]
Code not maintainable – twice more effort
Testing and testable code ,[object Object],[object Object]
After-party conclusion  ,[object Object]
I don't use TDD because ,[object Object]
Code not maintainable – twice more effort ,[object Object]
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object]
It's not about testing, it's about requirements and design
Testing and testable code ,[object Object],[object Object]
It's not about testing, it's about requirements and design
Large tests and relatively high coverage are just positive side effects
Testing and testable code ,[object Object],[object Object],[object Object]
TDD Architect
Testing and testable code ,[object Object],[object Object],[object Object]
TDD Architect ” Test Driven Development is like sex. If you don't like it, you probably ain't doing it right.”
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest'
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo...
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object]
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object],[object Object]
User need to have login and password fields
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object],[object Object]
User need to have login and password fields
Getters and setters!
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object],[object Object]
User need to have login and password fields
Getters and setters!
Equals and hashCode methods for equality
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login fo... ,[object Object],[object Object]
User need to have login and password fields
Getters and setters!
Equals and hashCode methods for equality ,[object Object]
Testing and testable code @Test public void testUserCreation() throws Exception { User user = new User(); }
Testing and testable code @Test public void testUserHasLoginAndPassword() throws Exception { User user = new User(); user.setLogin("login"); user.setPassword("password"); assertEquals("login", user.getLogin()); assertEquals("password", user.getPassword()); }
Testing and testable code @Test public void testEqualsMethodValidForSameLogin() throws Exception { User user1 = new User(); user1.setLogin("login"); User user2 = new User(); user2.setLogin("login"); assertEquals(user1, user2); }
Testing and testable code @Test public void testEqualsMethodReturnsFalseForDifferentLogin() throws    Exception { User user1 = new User(); user1.setLogin("login"); User user2 = new User(); user2.setLogin("login2"); assertFalse(user1.equals(user2)); }
Testing and testable code @Test public void testEqualsHashCodeConstract() throws Exception { User user1 = new User(); user1.setLogin("login"); User user2 = new User(); user2.setLogin("login"); assertTrue(user1.equals(user2)); assertEquals(user1.hashCode(),user2.hashCode()); }
Testing and testable code public class User { private String login, password; public User() {  } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!login.equals(user.login)) return false; return true; } public int hashCode() { return login.hashCode(); } } Design you get:
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest'
Testing and testable code ,[object Object],[object Object],[object Object]
Should not login for incorrect login
Should not login for incorrect password
Not logged in user is an 'guest user'
Guest user has login 'Guest'
Testing and testable code …  the test should really looked like this: @Test public void shouldLoginForCorrectLoginAndPassword() throws Exception{ // given String login = "login"; String password = "password"; dao.persist(new User(login,password)); // when User user = service.logIn(login, password); // then assertEquals(login, user.getLogin()); assertEquals(password, user.getPassword()); }
Testing and testable code public class User { private String login, password; public User(String login,  String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public String getPassword() { return password; } } Design you get:
Testing and testable code public class User { private String login, password; public User() {  } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (!login.equals(user.login)) return false; return true; } public int hashCode() { return login.hashCode(); } } public class User { private String login, password; public User(String login,  String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public String getPassword() { return password; } }
Testing and testable code ,[object Object],[object Object],[object Object]
Smallest change in your code make a lot of tests fail, even if that change isn't caused be change of requirements
Some of the code might never be used. Ever! ,[object Object],[object Object]
Created design based on experience only.
Design is complex, not maintainable
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object],[object Object]
Testing and testable code ,[object Object],[object Object],[object Object],[object Object],[object Object]
Testing and testable code ,[object Object]
Testing and testable code ,[object Object]
Symptoms of being TDD Prophet: ,[object Object]
Asking questions on Internet like: ,[object Object]
Is 75% code coverage good enough?
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object]
Create basic architecture using interfaces
Testing and testable code ,[object Object],[object Object]
Create basic architecture using interfaces
Start writing tests while implementing previously designed interfaces
Testing and testable code ,[object Object],[object Object]
Testing and testable code ,[object Object],[object Object]
Bigger chances that code will do what client wants (requirements)
Will not stand a chance when the requirements change (design)
Testing and testable code ,[object Object],[object Object]
Bigger chances that code will do what client wants (requirements)
Will not stand a chance when the requirements change (design)
Again: not a TDD practice!
Testing and testable code ,[object Object]
Testing and testable code ,[object Object],[object Object]
Run all tests and see the new one failing
Add some code
Run all tests and see the new one succeeds
Refactor
Testing and testable code ,[object Object],[object Object]
Add test
Run all tests and see the new one failing

Mais conteúdo relacionado

Destaque

Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantesResultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
marlosa75
 
Joseph Gigliotti - Resume (Linked-In)
Joseph Gigliotti - Resume (Linked-In)Joseph Gigliotti - Resume (Linked-In)
Joseph Gigliotti - Resume (Linked-In)
Joseph Gigliotti
 
Презентація:Матеріали до уроків
Презентація:Матеріали до уроківПрезентація:Матеріали до уроків
Презентація:Матеріали до уроків
sveta7940
 
Economía e instituciones - Una mirada Objetiva
Economía e instituciones - Una mirada ObjetivaEconomía e instituciones - Una mirada Objetiva
Economía e instituciones - Una mirada Objetiva
mario171985
 

Destaque (20)

Reducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer ExampleReducing Boilerplate and Combining Effects: A Monad Transformer Example
Reducing Boilerplate and Combining Effects: A Monad Transformer Example
 
Make your programs Free
Make your programs FreeMake your programs Free
Make your programs Free
 
Presentacion clase 1
Presentacion clase 1Presentacion clase 1
Presentacion clase 1
 
Postgres tutorial
Postgres tutorial Postgres tutorial
Postgres tutorial
 
Texto Marcelo Lagos
Texto Marcelo LagosTexto Marcelo Lagos
Texto Marcelo Lagos
 
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantesResultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
 
Lopez de micay. información del micrositio
Lopez de micay. información del micrositioLopez de micay. información del micrositio
Lopez de micay. información del micrositio
 
Estatuto del representante legal
Estatuto del representante legalEstatuto del representante legal
Estatuto del representante legal
 
OTP application (with gen server child) - simple example
OTP application (with gen server child) - simple exampleOTP application (with gen server child) - simple example
OTP application (with gen server child) - simple example
 
Trabajo del tema 14
Trabajo del tema 14Trabajo del tema 14
Trabajo del tema 14
 
Salut!
Salut!Salut!
Salut!
 
Emprender en la Actualidad - JECA 2015 Universidad Nacional del Sur
Emprender en la Actualidad - JECA 2015 Universidad Nacional del SurEmprender en la Actualidad - JECA 2015 Universidad Nacional del Sur
Emprender en la Actualidad - JECA 2015 Universidad Nacional del Sur
 
Joseph Gigliotti - Resume (Linked-In)
Joseph Gigliotti - Resume (Linked-In)Joseph Gigliotti - Resume (Linked-In)
Joseph Gigliotti - Resume (Linked-In)
 
Estrategias de aprendizaje (estudios de casos)
Estrategias de aprendizaje (estudios de casos)Estrategias de aprendizaje (estudios de casos)
Estrategias de aprendizaje (estudios de casos)
 
Tbe.03
Tbe.03Tbe.03
Tbe.03
 
9781 Kasobranie
9781 Kasobranie9781 Kasobranie
9781 Kasobranie
 
Shell nmdl1
Shell nmdl1Shell nmdl1
Shell nmdl1
 
Презентація:Матеріали до уроків
Презентація:Матеріали до уроківПрезентація:Матеріали до уроків
Презентація:Матеріали до уроків
 
Economía e instituciones - Una mirada Objetiva
Economía e instituciones - Una mirada ObjetivaEconomía e instituciones - Una mirada Objetiva
Economía e instituciones - Una mirada Objetiva
 
Perfil de tu facilitador
Perfil de tu facilitadorPerfil de tu facilitador
Perfil de tu facilitador
 

Semelhante a Testing and Testable Code

Spec flow – functional testing made easy
Spec flow – functional testing made easySpec flow – functional testing made easy
Spec flow – functional testing made easy
Paul Stack
 
The Testing Planet Issue 2
The Testing Planet Issue 2The Testing Planet Issue 2
The Testing Planet Issue 2
Rosie Sherry
 
Agile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practiceAgile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practice
denis Udod
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim
 

Semelhante a Testing and Testable Code (20)

Code Quality Practice and Tools
Code Quality Practice and ToolsCode Quality Practice and Tools
Code Quality Practice and Tools
 
BDD Primer
BDD PrimerBDD Primer
BDD Primer
 
TDD Walkthrough - Encryption
TDD Walkthrough - EncryptionTDD Walkthrough - Encryption
TDD Walkthrough - Encryption
 
Art of unit testing: how to do it right
Art of unit testing: how to do it rightArt of unit testing: how to do it right
Art of unit testing: how to do it right
 
Spec flow – functional testing made easy
Spec flow – functional testing made easySpec flow – functional testing made easy
Spec flow – functional testing made easy
 
Refactoring legacy code
Refactoring legacy codeRefactoring legacy code
Refactoring legacy code
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
The Testing Planet Issue 2
The Testing Planet Issue 2The Testing Planet Issue 2
The Testing Planet Issue 2
 
Agile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practiceAgile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practice
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
50 Shades of WordPress
50 Shades of WordPress50 Shades of WordPress
50 Shades of WordPress
 
I, For One, Welcome Our New Robot Overlords
I, For One, Welcome Our New Robot OverlordsI, For One, Welcome Our New Robot Overlords
I, For One, Welcome Our New Robot Overlords
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
Testable requirements
Testable requirementsTestable requirements
Testable requirements
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 

Mais de Pawel Szulc

Mais de Pawel Szulc (20)

Getting acquainted with Lens
Getting acquainted with LensGetting acquainted with Lens
Getting acquainted with Lens
 
Impossibility
ImpossibilityImpossibility
Impossibility
 
Maintainable Software Architecture in Haskell (with Polysemy)
Maintainable Software Architecture in Haskell (with Polysemy)Maintainable Software Architecture in Haskell (with Polysemy)
Maintainable Software Architecture in Haskell (with Polysemy)
 
Painless Haskell
Painless HaskellPainless Haskell
Painless Haskell
 
Trip with monads
Trip with monadsTrip with monads
Trip with monads
 
Trip with monads
Trip with monadsTrip with monads
Trip with monads
 
Illogical engineers
Illogical engineersIllogical engineers
Illogical engineers
 
RChain - Understanding Distributed Calculi
RChain - Understanding Distributed CalculiRChain - Understanding Distributed Calculi
RChain - Understanding Distributed Calculi
 
Illogical engineers
Illogical engineersIllogical engineers
Illogical engineers
 
Understanding distributed calculi in Haskell
Understanding distributed calculi in HaskellUnderstanding distributed calculi in Haskell
Understanding distributed calculi in Haskell
 
Software engineering the genesis
Software engineering  the genesisSoftware engineering  the genesis
Software engineering the genesis
 
Going bananas with recursion schemes for fixed point data types
Going bananas with recursion schemes for fixed point data typesGoing bananas with recursion schemes for fixed point data types
Going bananas with recursion schemes for fixed point data types
 
“Going bananas with recursion schemes for fixed point data types”
“Going bananas with recursion schemes for fixed point data types”“Going bananas with recursion schemes for fixed point data types”
“Going bananas with recursion schemes for fixed point data types”
 
Writing your own RDD for fun and profit
Writing your own RDD for fun and profitWriting your own RDD for fun and profit
Writing your own RDD for fun and profit
 
The cats toolbox a quick tour of some basic typeclasses
The cats toolbox  a quick tour of some basic typeclassesThe cats toolbox  a quick tour of some basic typeclasses
The cats toolbox a quick tour of some basic typeclasses
 
Introduction to type classes
Introduction to type classesIntroduction to type classes
Introduction to type classes
 
Functional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heavenFunctional Programming & Event Sourcing - a pair made in heaven
Functional Programming & Event Sourcing - a pair made in heaven
 
Apache spark workshop
Apache spark workshopApache spark workshop
Apache spark workshop
 
Introduction to type classes in 30 min
Introduction to type classes in 30 minIntroduction to type classes in 30 min
Introduction to type classes in 30 min
 
Real world gobbledygook
Real world gobbledygookReal world gobbledygook
Real world gobbledygook
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+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@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+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...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Testing and Testable Code