SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
When TDD Goes Awry
Clueless tests, infesting
mocks and other horrors...
A voyage into today Java
Enterprise worse practices.

Uberto	
  Barbini
@ramtop

h0ps://github.com/uberto

Friday, November 1, 13
About	
  me
My	
  first	
  program

SHO
to	
  start
SHIN
Heart,	
  mind

In the beginner's mind there are many possibilities,
in the expert's mind there are few.
Friday, November 1, 13
a·wry (-r)
adv.
1. In a position that is turned or twisted toward one
side; askew.
2. Away from the correct course; amiss.

Friday, November 1, 13
a·wry (-r)
adv.
1. In a position that is turned or twisted toward one
side; askew.
2. Away from the correct course; amiss.
     @Test
     public void testGetSwapType_SPOTFWD()
     {
          when(mockTrade.getField(FXSubmitFields.SWAP_TYPE)).thenReturn("SPOTFWD");
          setUpTrade("SWAP");
          assertEquals(FXSwapType.SPOTFWD, trade.getSwapType());
         
         
         
     }
    
         
     {
         
         
         
         
     }

Friday, November 1, 13

when(mockTrade.getField(FXSubmitFields.SWAP_TYPE)).thenReturn("SPOTFWD");
setUpTrade("FWDFWDSWAP");
assertEquals(FXSwapType.SPOTFWD, trade.getSwapType());
private void setUpTrade(final String tradingType)
when(mockTrade.getField(ACCOUNT)).thenReturn(ACCOUNT_VAL);
when(mockTrade.getField(CURRENCY_PAIR)).thenReturn(CURRENCY_PAIR_VAL);
when(mockTrade.getField(TRADING_TYPE)).thenReturn(tradingType);
trade = new FXTrade(mockTrade, USER, mockNearLeg, mockFarLeg);
Test Stories
Each test should tell a story
Scenario tests illustrate the design

Friday, November 1, 13
Test Stories
Each test should tell a story
Scenario tests illustrate the design

When you are thinking big thoughts, write big tests.
When you are thinking little thoughts, write little tests.
Kent Beck, Quora

Friday, November 1, 13
Test Stories
Each test should tell a story
Scenario tests illustrate the design

When you are thinking big thoughts, write big tests.
When you are thinking little thoughts, write little tests.
Kent Beck, Quora

Objects are nouns. Good design is a good story.
Do you remember XP Metaphor?
Friday, November 1, 13
2001
Friday, November 1, 13
My first project

2001
Friday, November 1, 13
My first project

2001
Meaningful test names

Friday, November 1, 13
Test Driven Design
It’s a kind of design technique, not a way to test.

When TDD is not useful:
when your don’t care about design
ie. technical spikes, learning exercises

Friday, November 1, 13
I get paid for code that works, not for tests, so
my philosophy is to test as little as possible to
reach a given level of confidence.
Kent Beck Stackoverflow

Test Driven Design
It’s a kind of design technique, not a way to test.

When TDD is not useful:
when your don’t care about design
ie. technical spikes, learning exercises

Friday, November 1, 13
Question:
Why designing for testability result
in good design?
The caveman house design
Carlo Pescio

Friday, November 1, 13
Question:
Why designing for testability result
in good design?
The caveman house design
Carlo Pescio

Global state
Hidden dependencies
Inflexible behavior
Friday, November 1, 13

Things that work together
are kept close
Let’s start from Assertions
One of the least followed TTD rule says: “There must be
one assertion for test”. Why?
The point behind testing one thing at time is the we want to
run all the state checks, every time independently.
No IF in the tests.
No logic in the tests, much less duplication with tested logic.

Friday, November 1, 13
Let’s start from Assertions
One of the least followed TTD rule says: “There must be
one assertion for test”. Why?
The point behind testing one thing at time is the we want to
run all the state checks, every time independently.
No IF in the tests.
No logic in the tests, much less duplication with tested logic.

3 typical reasons for many assertions in a test...

Friday, November 1, 13
Assertion Code

Friday, November 1, 13
Mocking rules
At most a single mock and many stubs.
Use stubs for internals and close friends, mocks
for collaborators (i.e. listeners)
Stubs can be prepared in setup or with builder
helpers. Mocks in the actual test.
Try to verify mocks with actual params or matcher,
not any (or maybe you wanted a stub instead?).

Friday, November 1, 13
Mock-o-meter

012345

Friday, November 1, 13
Mock-o-meter

012345
If to test 3 lines of simple code, we have
10 lines of complicated test with mocks.
Which is more likely to have a bug? the
code or the test?

Friday, November 1, 13
Mocks Code

Friday, November 1, 13
High Coupling
In software engineering, coupling or dependency is the degree to which
each program module relies on each one of the other modules.
antipattern of high coupling:
cohesion refers to the degree to which the elements of a module belong
together.[1] Thus, it is a measure of how strongly-related each piece of
functionality expressed by the source code of a software module is.
Wikipedia

Friday, November 1, 13
A little digression:
Dependency Injection frameworks
The best classes in any application are the ones that do stuff: the
BarcodeDecoder, the KoopaPhysicsEngine, and theAudioStreamer. These
classes have dependencies; perhaps a BarcodeCameraFinder,
DefaultPhysicsEngine, and anHttpStreamer.
To contrast, the worst classes in any application are the ones that take up space
without doing much at all: theBarcodeDecoderFactory, the
CameraServiceLoader, and the MutableContextWrapper. These classes are
the clumsy duct tape that wires the interesting stuff together.
Dagger is a replacement for these FactoryFactory classes. It allows you to focus
on the interesting classes. Declare dependencies, specify how to satisfy them, and
ship your app.

from Dagger introduction
http://square.github.io/dagger/

Friday, November 1, 13

Good things about Dagger:
good and invisible duct tape
Duct Tape is important!

Friday, November 1, 13
Duct Tape is important!
That is, it’s important to
wiring up our objects
in the best possible way.
Write tests to show how
your wiring is done
Replace Duct Tape with
Demeter

Friday, November 1, 13
High Coupling Code

Friday, November 1, 13
Lasagna Code
Lasagna code, coined in 1982 by Joe Celko, is a type of
program structure characterized by several well-defined
and separable layers, where each layer of code accesses
services in the layers below through well-defined interfaces.
[...] A quote usually attributed either to David Wheeler or
Butler Lampson reads, "There is no problem in computer
science that cannot be solved by adding another layer of
indirection, except having too many layers of indirection".

Friday, November 1, 13
Layer Code

Friday, November 1, 13
We have a problem,

Friday, November 1, 13
We have a problem,
Our code is too difficult to test

Friday, November 1, 13
We have a problem,
Our code is too difficult to test
Let's write a framework to test it!

Friday, November 1, 13
We have a problem,
Our code is too difficult to test
Let's write a framework to test it!
Ok, now we have 2 problems...

Friday, November 1, 13
We have a problem,
Our code is too difficult to test
Let's write a framework to test it!
Ok, now we have 2 problems...

Dedicated test stub must be simple and transparent.
They should explain the model, not hide it.
Friday, November 1, 13
We have a problem,
Our code is too difficult to test
Let's write a framework to test it!
Ok, now we have 2 problems...
Same problem for who has to develop against a
big framework: even if I have the framework tests,
how can I be sure of not losing pieces around?
Let's model domain simply as whole and then split
it up for the framework.

Dedicated test stub must be simple and transparent.
They should explain the model, not hide it.
Friday, November 1, 13
How to improve

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.
Ask to new team members or dev from other
teams their impressions.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.
Ask to new team members or dev from other
teams their impressions.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.
Ask to new team members or dev from other
teams their impressions.
Experiment and share.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.
Ask to new team members or dev from other
teams their impressions.
Experiment and share.

Friday, November 1, 13
How to improve
If your tests give you pain don't ignore it.
Localize the cause.
Ask to new team members or dev from other
teams their impressions.
Experiment and share.
Rule 0: TDD is supposed to be fun and simple.

Friday, November 1, 13

Más contenido relacionado

Similar a When Tdd Goes Awry

TDD with LEGO at SDEC13
TDD with LEGO at SDEC13TDD with LEGO at SDEC13
TDD with LEGO at SDEC13BillyGarnet
 
Random testing
Random testingRandom testing
Random testingLocaweb
 
When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)Uberto Barbini
 
Building for resilience (with speaking notes)
Building for resilience (with speaking notes)Building for resilience (with speaking notes)
Building for resilience (with speaking notes)Abe Gong
 
Writing testable code
Writing testable codeWriting testable code
Writing testable codeAlvaro Videla
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextElisabeth Hendrickson
 
Finding Some "Good" iOS Interview Questions for Employers
Finding Some "Good" iOS Interview Questions for EmployersFinding Some "Good" iOS Interview Questions for Employers
Finding Some "Good" iOS Interview Questions for Employersnataliepo
 
Developing Software with Security in Mind
Developing Software with Security in MindDeveloping Software with Security in Mind
Developing Software with Security in Mindsblom
 
Reactive design: languages, and paradigms
Reactive design: languages, and paradigmsReactive design: languages, and paradigms
Reactive design: languages, and paradigmsDean Wampler
 
Selenium Users Anonymous
Selenium Users AnonymousSelenium Users Anonymous
Selenium Users AnonymousDave Haeffner
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic RevisitedAdam Keys
 
Hack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programingHack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programingunihack
 
Metric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleMetric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleSteve Karam
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easierChristian Hujer
 

Similar a When Tdd Goes Awry (20)

TDD with LEGO at SDEC13
TDD with LEGO at SDEC13TDD with LEGO at SDEC13
TDD with LEGO at SDEC13
 
Test Driven Development - Caleb Tutty
Test Driven Development - Caleb TuttyTest Driven Development - Caleb Tutty
Test Driven Development - Caleb Tutty
 
Random testing
Random testingRandom testing
Random testing
 
When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)
 
B D D Intro
B D D  IntroB D D  Intro
B D D Intro
 
Building for resilience (with speaking notes)
Building for resilience (with speaking notes)Building for resilience (with speaking notes)
Building for resilience (with speaking notes)
 
Writing testable code
Writing testable codeWriting testable code
Writing testable code
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile Context
 
Finding Some "Good" iOS Interview Questions for Employers
Finding Some "Good" iOS Interview Questions for EmployersFinding Some "Good" iOS Interview Questions for Employers
Finding Some "Good" iOS Interview Questions for Employers
 
Developing Software with Security in Mind
Developing Software with Security in MindDeveloping Software with Security in Mind
Developing Software with Security in Mind
 
Reactive design: languages, and paradigms
Reactive design: languages, and paradigmsReactive design: languages, and paradigms
Reactive design: languages, and paradigms
 
Agile Practices
Agile PracticesAgile Practices
Agile Practices
 
Selenium Users Anonymous
Selenium Users AnonymousSelenium Users Anonymous
Selenium Users Anonymous
 
Culture And Aesthetic Revisited
Culture And Aesthetic RevisitedCulture And Aesthetic Revisited
Culture And Aesthetic Revisited
 
TDD and Getting Paid
TDD and Getting PaidTDD and Getting Paid
TDD and Getting Paid
 
Testable Code ... In Joomla!?
Testable Code ... In Joomla!?Testable Code ... In Joomla!?
Testable Code ... In Joomla!?
 
Hack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programingHack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programing
 
Metric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleMetric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in Oracle
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
 
Agile framework Support
Agile framework SupportAgile framework Support
Agile framework Support
 

Más de Uberto Barbini

CQRS with Event Source in Functional sauce served by Kotlin
CQRS with Event Source in Functional sauce served by Kotlin CQRS with Event Source in Functional sauce served by Kotlin
CQRS with Event Source in Functional sauce served by Kotlin Uberto Barbini
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About MorphismsUberto Barbini
 
The Role of Testing in DevOps
The Role of Testing in DevOpsThe Role of Testing in DevOps
The Role of Testing in DevOpsUberto Barbini
 
Boost your-oop-with-fp
Boost your-oop-with-fpBoost your-oop-with-fp
Boost your-oop-with-fpUberto Barbini
 
Develop Gwt application in TDD
Develop Gwt application in TDDDevelop Gwt application in TDD
Develop Gwt application in TDDUberto Barbini
 

Más de Uberto Barbini (8)

CQRS with Event Source in Functional sauce served by Kotlin
CQRS with Event Source in Functional sauce served by Kotlin CQRS with Event Source in Functional sauce served by Kotlin
CQRS with Event Source in Functional sauce served by Kotlin
 
Go kotlin, Go!
Go kotlin, Go!Go kotlin, Go!
Go kotlin, Go!
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
 
Legacy is Good
Legacy is GoodLegacy is Good
Legacy is Good
 
The Role of Testing in DevOps
The Role of Testing in DevOpsThe Role of Testing in DevOps
The Role of Testing in DevOps
 
Boost your-oop-with-fp
Boost your-oop-with-fpBoost your-oop-with-fp
Boost your-oop-with-fp
 
The Effective Team
The Effective TeamThe Effective Team
The Effective Team
 
Develop Gwt application in TDD
Develop Gwt application in TDDDevelop Gwt application in TDD
Develop Gwt application in TDD
 

Ú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 FMESafe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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.pptxRustici Software
 
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 2024The Digital Insurer
 
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 2024The Digital Insurer
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
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 REVIEWERMadyBayot
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
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 FMESafe Software
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 

Último (20)

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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
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
 
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
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

When Tdd Goes Awry

  • 1. When TDD Goes Awry Clueless tests, infesting mocks and other horrors... A voyage into today Java Enterprise worse practices. Uberto  Barbini @ramtop h0ps://github.com/uberto Friday, November 1, 13
  • 2. About  me My  first  program SHO to  start SHIN Heart,  mind In the beginner's mind there are many possibilities, in the expert's mind there are few. Friday, November 1, 13
  • 3. a·wry (-r) adv. 1. In a position that is turned or twisted toward one side; askew. 2. Away from the correct course; amiss. Friday, November 1, 13
  • 4. a·wry (-r) adv. 1. In a position that is turned or twisted toward one side; askew. 2. Away from the correct course; amiss.      @Test      public void testGetSwapType_SPOTFWD()      {           when(mockTrade.getField(FXSubmitFields.SWAP_TYPE)).thenReturn("SPOTFWD");           setUpTrade("SWAP");           assertEquals(FXSwapType.SPOTFWD, trade.getSwapType());                                    }                     {                                              } Friday, November 1, 13 when(mockTrade.getField(FXSubmitFields.SWAP_TYPE)).thenReturn("SPOTFWD"); setUpTrade("FWDFWDSWAP"); assertEquals(FXSwapType.SPOTFWD, trade.getSwapType()); private void setUpTrade(final String tradingType) when(mockTrade.getField(ACCOUNT)).thenReturn(ACCOUNT_VAL); when(mockTrade.getField(CURRENCY_PAIR)).thenReturn(CURRENCY_PAIR_VAL); when(mockTrade.getField(TRADING_TYPE)).thenReturn(tradingType); trade = new FXTrade(mockTrade, USER, mockNearLeg, mockFarLeg);
  • 5. Test Stories Each test should tell a story Scenario tests illustrate the design Friday, November 1, 13
  • 6. Test Stories Each test should tell a story Scenario tests illustrate the design When you are thinking big thoughts, write big tests. When you are thinking little thoughts, write little tests. Kent Beck, Quora Friday, November 1, 13
  • 7. Test Stories Each test should tell a story Scenario tests illustrate the design When you are thinking big thoughts, write big tests. When you are thinking little thoughts, write little tests. Kent Beck, Quora Objects are nouns. Good design is a good story. Do you remember XP Metaphor? Friday, November 1, 13
  • 10. My first project 2001 Meaningful test names Friday, November 1, 13
  • 11. Test Driven Design It’s a kind of design technique, not a way to test. When TDD is not useful: when your don’t care about design ie. technical spikes, learning exercises Friday, November 1, 13
  • 12. I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence. Kent Beck Stackoverflow Test Driven Design It’s a kind of design technique, not a way to test. When TDD is not useful: when your don’t care about design ie. technical spikes, learning exercises Friday, November 1, 13
  • 13. Question: Why designing for testability result in good design? The caveman house design Carlo Pescio Friday, November 1, 13
  • 14. Question: Why designing for testability result in good design? The caveman house design Carlo Pescio Global state Hidden dependencies Inflexible behavior Friday, November 1, 13 Things that work together are kept close
  • 15. Let’s start from Assertions One of the least followed TTD rule says: “There must be one assertion for test”. Why? The point behind testing one thing at time is the we want to run all the state checks, every time independently. No IF in the tests. No logic in the tests, much less duplication with tested logic. Friday, November 1, 13
  • 16. Let’s start from Assertions One of the least followed TTD rule says: “There must be one assertion for test”. Why? The point behind testing one thing at time is the we want to run all the state checks, every time independently. No IF in the tests. No logic in the tests, much less duplication with tested logic. 3 typical reasons for many assertions in a test... Friday, November 1, 13
  • 18. Mocking rules At most a single mock and many stubs. Use stubs for internals and close friends, mocks for collaborators (i.e. listeners) Stubs can be prepared in setup or with builder helpers. Mocks in the actual test. Try to verify mocks with actual params or matcher, not any (or maybe you wanted a stub instead?). Friday, November 1, 13
  • 20. Mock-o-meter 012345 If to test 3 lines of simple code, we have 10 lines of complicated test with mocks. Which is more likely to have a bug? the code or the test? Friday, November 1, 13
  • 22. High Coupling In software engineering, coupling or dependency is the degree to which each program module relies on each one of the other modules. antipattern of high coupling: cohesion refers to the degree to which the elements of a module belong together.[1] Thus, it is a measure of how strongly-related each piece of functionality expressed by the source code of a software module is. Wikipedia Friday, November 1, 13
  • 23. A little digression: Dependency Injection frameworks The best classes in any application are the ones that do stuff: the BarcodeDecoder, the KoopaPhysicsEngine, and theAudioStreamer. These classes have dependencies; perhaps a BarcodeCameraFinder, DefaultPhysicsEngine, and anHttpStreamer. To contrast, the worst classes in any application are the ones that take up space without doing much at all: theBarcodeDecoderFactory, the CameraServiceLoader, and the MutableContextWrapper. These classes are the clumsy duct tape that wires the interesting stuff together. Dagger is a replacement for these FactoryFactory classes. It allows you to focus on the interesting classes. Declare dependencies, specify how to satisfy them, and ship your app. from Dagger introduction http://square.github.io/dagger/ Friday, November 1, 13 Good things about Dagger: good and invisible duct tape
  • 24. Duct Tape is important! Friday, November 1, 13
  • 25. Duct Tape is important! That is, it’s important to wiring up our objects in the best possible way. Write tests to show how your wiring is done Replace Duct Tape with Demeter Friday, November 1, 13
  • 26. High Coupling Code Friday, November 1, 13
  • 27. Lasagna Code Lasagna code, coined in 1982 by Joe Celko, is a type of program structure characterized by several well-defined and separable layers, where each layer of code accesses services in the layers below through well-defined interfaces. [...] A quote usually attributed either to David Wheeler or Butler Lampson reads, "There is no problem in computer science that cannot be solved by adding another layer of indirection, except having too many layers of indirection". Friday, November 1, 13
  • 29. We have a problem, Friday, November 1, 13
  • 30. We have a problem, Our code is too difficult to test Friday, November 1, 13
  • 31. We have a problem, Our code is too difficult to test Let's write a framework to test it! Friday, November 1, 13
  • 32. We have a problem, Our code is too difficult to test Let's write a framework to test it! Ok, now we have 2 problems... Friday, November 1, 13
  • 33. We have a problem, Our code is too difficult to test Let's write a framework to test it! Ok, now we have 2 problems... Dedicated test stub must be simple and transparent. They should explain the model, not hide it. Friday, November 1, 13
  • 34. We have a problem, Our code is too difficult to test Let's write a framework to test it! Ok, now we have 2 problems... Same problem for who has to develop against a big framework: even if I have the framework tests, how can I be sure of not losing pieces around? Let's model domain simply as whole and then split it up for the framework. Dedicated test stub must be simple and transparent. They should explain the model, not hide it. Friday, November 1, 13
  • 35. How to improve Friday, November 1, 13
  • 36. How to improve If your tests give you pain don't ignore it. Localize the cause. Friday, November 1, 13
  • 37. How to improve If your tests give you pain don't ignore it. Localize the cause. Friday, November 1, 13
  • 38. How to improve If your tests give you pain don't ignore it. Localize the cause. Ask to new team members or dev from other teams their impressions. Friday, November 1, 13
  • 39. How to improve If your tests give you pain don't ignore it. Localize the cause. Ask to new team members or dev from other teams their impressions. Friday, November 1, 13
  • 40. How to improve If your tests give you pain don't ignore it. Localize the cause. Ask to new team members or dev from other teams their impressions. Experiment and share. Friday, November 1, 13
  • 41. How to improve If your tests give you pain don't ignore it. Localize the cause. Ask to new team members or dev from other teams their impressions. Experiment and share. Friday, November 1, 13
  • 42. How to improve If your tests give you pain don't ignore it. Localize the cause. Ask to new team members or dev from other teams their impressions. Experiment and share. Rule 0: TDD is supposed to be fun and simple. Friday, November 1, 13