Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final

Mock Objects in Action 
Paulo Caroli & Sudhindra Rao 
Agile 2009 
© ThoughtWorks 2008
About us
About us 
Paulo Caroli 
& 
Sudhindra Rao
About us 
Hands-on developers
About us 
Open Source
About us 
ThoughtWorks
About us 
TDD enthusiasts
Agenda
Agenda 
• History 
• Design for Testability 
• Dependent Components 
• Mock Objects in Action 
• Top Offenders 
• Q&A
History
Once upon a time we started writing 
Unit Tests…
Then we started using unit tests 
frameworks…
And soon after we started creating (and 
re-creating) data and implementations 
to help drive our tests…
Then we came across Mock Objects 
frameworks…
But frameworks don’t work by 
themselves!
Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final
public class Greeting { 
private Translator translator; 
private final String HELLO = "Hello"; 
public Greeting() { 
this.translator = new TranslatorImpl(); 
} 
public String sayHello(String language, String name) { 
String message = HELLO; 
// no need to call translator if the language is English 
if (!language.equals("English")){ 
message = translator.translate("English", language, HELLO ); 
} 
return message + " " + name; 
} 
}
Design for Testability
Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final
© ThoughtWorks 2008 
public class Greeting { 
private Translator translator; 
private final String HELLO = "Hello"; 
public Greeting(Translator translator) { 
this.translator = translator; 
} 
public String sayHello(String language, String name) { 
String message = HELLO; 
// no need to call translator if the language is English 
if (!language.equals("English")){ 
message = translator.translate("English", language, HELLO ); 
} 
return message + " " + name; 
} 
}
Mock Objects 
Mock Objects enable 
your unit test to mimic behavior and 
verify expectations on dependent 
components.
Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final
import static org.mockito.Mockito.*; 
public class GreetingTest{ 
@Test 
public void shouldGreetInSomeLanguage(){ 
// setup 
Translator translator = mock(Translator.class); 
Greeting greeting = new Greeting(translator); 
when(translator.translate("English", "French”,"Hello")). 
thenReturn("Bonjour"); 
// execute 
assertEquals("Bonjour Madame", 
greeting.sayHello("French", "Madame")); 
// verify 
verify(translator).translate("English", "French", "Hello"); 
} 
}
Mock Objects 
Mock Objects enable 
your unit test to mimic behavior and 
verify expectations on dependent 
components.
Mock Objects 
Mock Objects enable 
your unit test to mimic behavior and 
verify expectations on dependent 
components.
Dependent Components
Component 
Component 
© ThoughtWorks 2008 
A 
A 
Component 
Component 
B 
B 
Dependent 
Components
Object Graphs
Third Party
Very Slow
Remote 
Component
Database 
Component
Circular 
Dependency
Complex 
Set-up
Exceptional 
Behavior
Time Dependent 
Behavior
My co-worker's 
component
Mock Objects In Action
© ThoughtWorks 2008
Mock Objects 
in Action
Mock Objects 
in Action
Code Watch
Mock Objects 
Top Offenders
Test readability
Replacement for 
integration test
Unnecessary 
Mock (ex: 
simple Value 
Object)
Mock context confusion
Mock return a Mock
Abuse of mocks 
(instead: FakeDB, Dummy data, Stub, 
Test Fixture)
Blaming the framework
Mock Objects in Action 
Thanks for attending! 
Questions? 
Paulo Caroli & Sudhindra Rao 
pcaroli@thoughtworks.com 
srao@thoughtworks.com
Recommended links 
http://xunitpatterns.com/Mock%20Object.html 
http://developer-in-test.blogspot.com/ 
http://misko.hevery.com/code-reviewers-guide/ 
http://codebetter.com/blogs/jeremy.miller/archive/2006/01/10/136407.aspx 
http://martinfowler.com/articles/mocksArentStubs.html 
http://www.jmock.org/oopsla2004.pdf 
http://mocksamples.com/
1 of 52

Recommended

Deep dive into Xtext scoping local and global scopes explained by
Deep dive into Xtext scoping local and global scopes explainedDeep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explainedHolger Schill
3.4K views74 slides
Formatting ForThe Masses by
Formatting ForThe MassesFormatting ForThe Masses
Formatting ForThe MassesHolger Schill
477 views58 slides
Building unit tests correctly with visual studio 2013 by
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Dror Helper
2.8K views46 slides
Advanced JavaScript - Internship Presentation - Week6 by
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Devang Garach
165 views21 slides
Prototype Js by
Prototype JsPrototype Js
Prototype JsKivanc Kanturk
187 views14 slides
Working Effectively with Legacy Code: Lessons in Practice by
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeAmar Shah
644 views33 slides

More Related Content

What's hot

Advanced JavaScript by
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
1.6K views41 slides
Why Your Test Suite Sucks - PHPCon PL 2015 by
Why Your Test Suite Sucks - PHPCon PL 2015Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015CiaranMcNulty
1.2K views73 slides
SOLID, DRY, SLAP design principles by
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSergey Karpushin
17.8K views69 slides
Extending C# with Roslyn and Code Aware Libraries by
Extending C# with Roslyn and Code Aware LibrariesExtending C# with Roslyn and Code Aware Libraries
Extending C# with Roslyn and Code Aware LibrariesCarlo Pescio
881 views38 slides
Working Effectively with Legacy Code by
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Codeslicklash
688 views49 slides
TDD with phpspec2 by
TDD with phpspec2TDD with phpspec2
TDD with phpspec2Anton Serdyuk
4.8K views76 slides

What's hot(16)

Advanced JavaScript by Fu Cheng
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Fu Cheng1.6K views
Why Your Test Suite Sucks - PHPCon PL 2015 by CiaranMcNulty
Why Your Test Suite Sucks - PHPCon PL 2015Why Your Test Suite Sucks - PHPCon PL 2015
Why Your Test Suite Sucks - PHPCon PL 2015
CiaranMcNulty1.2K views
SOLID, DRY, SLAP design principles by Sergey Karpushin
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
Sergey Karpushin17.8K views
Extending C# with Roslyn and Code Aware Libraries by Carlo Pescio
Extending C# with Roslyn and Code Aware LibrariesExtending C# with Roslyn and Code Aware Libraries
Extending C# with Roslyn and Code Aware Libraries
Carlo Pescio881 views
Working Effectively with Legacy Code by slicklash
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Code
slicklash688 views
[教材] 例外處理設計與重構實作班201309 by teddysoft
[教材] 例外處理設計與重構實作班201309[教材] 例外處理設計與重構實作班201309
[教材] 例外處理設計與重構實作班201309
teddysoft14.9K views
Driving Design with PhpSpec by CiaranMcNulty
Driving Design with PhpSpecDriving Design with PhpSpec
Driving Design with PhpSpec
CiaranMcNulty1.7K views
TDD with PhpSpec - Lone Star PHP 2016 by CiaranMcNulty
TDD with PhpSpec - Lone Star PHP 2016TDD with PhpSpec - Lone Star PHP 2016
TDD with PhpSpec - Lone Star PHP 2016
CiaranMcNulty2.4K views
An Introduction to Processing by Cate Huston
An Introduction to ProcessingAn Introduction to Processing
An Introduction to Processing
Cate Huston3.3K views

Viewers also liked

TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra? by
TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra?	TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra?
TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra? tdc-globalcode
918 views26 slides
Atualização de status do projeto by
Atualização de status do projetoAtualização de status do projeto
Atualização de status do projetoGEP
1.1K views9 slides
Status Do Projeto by
Status Do ProjetoStatus Do Projeto
Status Do ProjetoRodolfo Rodrigo Acosta
1.9K views10 slides
Status+report by
Status+reportStatus+report
Status+reportMilton Santos
2.8K views9 slides
Agile br one_week_inception by
Agile br one_week_inceptionAgile br one_week_inception
Agile br one_week_inceptioncaetano_tc
5.7K views28 slides
Minimum Viable Product by
Minimum Viable ProductMinimum Viable Product
Minimum Viable ProductThoughtWorks Studios
1.5K views36 slides

Viewers also liked(12)

TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra? by tdc-globalcode
TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra?	TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra?
TDC2016POA | Trilha Dinamica - Facilitação - Que momento o grupo se encontra?
tdc-globalcode918 views
Atualização de status do projeto by GEP
Atualização de status do projetoAtualização de status do projeto
Atualização de status do projeto
GEP1.1K views
Agile br one_week_inception by caetano_tc
Agile br one_week_inceptionAgile br one_week_inception
Agile br one_week_inception
caetano_tc5.7K views
TDC2016SP - Trilha Análise de Negócios by tdc-globalcode
TDC2016SP - Trilha Análise de NegóciosTDC2016SP - Trilha Análise de Negócios
TDC2016SP - Trilha Análise de Negócios
tdc-globalcode299 views
How to be a successful agile product manager by Anupam Kundu
How to be a successful agile product managerHow to be a successful agile product manager
How to be a successful agile product manager
Anupam Kundu1.9K views
How to use agile for roadmapping and be successful at it by Anupam Kundu
How to use agile for roadmapping and be successful at itHow to use agile for roadmapping and be successful at it
How to use agile for roadmapping and be successful at it
Anupam Kundu5.1K views
[Business Agility] - Building the right thing and building it right. by Thoughtworks
[Business Agility] - Building the right thing and building it right.[Business Agility] - Building the right thing and building it right.
[Business Agility] - Building the right thing and building it right.
Thoughtworks3.6K views
Strategy to Execution by Jonny Schneider - ThoughtWorks by Thoughtworks
Strategy to Execution by Jonny Schneider - ThoughtWorksStrategy to Execution by Jonny Schneider - ThoughtWorks
Strategy to Execution by Jonny Schneider - ThoughtWorks
Thoughtworks31.1K views
7 Dimensions of Agile Analytics by Ken Collier by Thoughtworks
7 Dimensions of Agile Analytics by Ken Collier 7 Dimensions of Agile Analytics by Ken Collier
7 Dimensions of Agile Analytics by Ken Collier
Thoughtworks7.6K views

Similar to Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final

Refactoring In Tdd The Missing Part by
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartGabriele Lana
7.9K views97 slides
Bring the fun back to java by
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
640 views32 slides
Javascript Best Practices by
Javascript Best PracticesJavascript Best Practices
Javascript Best PracticesChristian Heilmann
129.9K views145 slides
Software Engineering by
Software EngineeringSoftware Engineering
Software EngineeringRobbie Clutton
766 views18 slides
201309 130917200320-phpapp01 by
201309 130917200320-phpapp01201309 130917200320-phpapp01
201309 130917200320-phpapp01Simon Lin
777 views112 slides
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014 by
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014FalafelSoftware
671 views52 slides

Similar to Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final(20)

Refactoring In Tdd The Missing Part by Gabriele Lana
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
Gabriele Lana7.9K views
Bring the fun back to java by ciklum_ods
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods640 views
201309 130917200320-phpapp01 by Simon Lin
201309 130917200320-phpapp01201309 130917200320-phpapp01
201309 130917200320-phpapp01
Simon Lin777 views
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014 by FalafelSoftware
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware671 views
Breaking Dependencies to Allow Unit Testing by Steven Smith
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith1.6K views
Developer Test - Things to Know by vilniusjug
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
vilniusjug781 views
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando... by Applitools
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Applitools1.2K views
Groovy DSLs (JavaOne Presentation) by Jim Driscoll
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)
Jim Driscoll2.2K views
Stopping the Rot - Putting Legacy C++ Under Test by Seb Rose
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
Seb Rose2.9K views
Drupal 8: A story of growing up and getting off the island by Angela Byron
Drupal 8: A story of growing up and getting off the islandDrupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the island
Angela Byron923 views
Breaking Dependencies to Allow Unit Testing by Steven Smith
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith1.4K views
Stencil: The Time for Vanilla Web Components has Arrived by Gil Fink
Stencil: The Time for Vanilla Web Components has ArrivedStencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink310 views

Recently uploaded

This talk was not generated with ChatGPT: how AI is changing science by
This talk was not generated with ChatGPT: how AI is changing scienceThis talk was not generated with ChatGPT: how AI is changing science
This talk was not generated with ChatGPT: how AI is changing scienceElena Simperl
34 views13 slides
"Running students' code in isolation. The hard way", Yurii Holiuk by
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk Fwdays
38 views34 slides
Future of AR - Facebook Presentation by
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook PresentationRob McCarty
66 views27 slides
Business Analyst Series 2023 - Week 4 Session 8 by
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8DianaGray10
180 views13 slides
The Power of Generative AI in Accelerating No Code Adoption.pdf by
The Power of Generative AI in Accelerating No Code Adoption.pdfThe Power of Generative AI in Accelerating No Code Adoption.pdf
The Power of Generative AI in Accelerating No Code Adoption.pdfSaeed Al Dhaheri
44 views18 slides
Inawisdom IDP by
Inawisdom IDPInawisdom IDP
Inawisdom IDPPhilipBasford
17 views48 slides

Recently uploaded(20)

This talk was not generated with ChatGPT: how AI is changing science by Elena Simperl
This talk was not generated with ChatGPT: how AI is changing scienceThis talk was not generated with ChatGPT: how AI is changing science
This talk was not generated with ChatGPT: how AI is changing science
Elena Simperl34 views
"Running students' code in isolation. The hard way", Yurii Holiuk by Fwdays
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk
Fwdays38 views
Future of AR - Facebook Presentation by Rob McCarty
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
Rob McCarty66 views
Business Analyst Series 2023 - Week 4 Session 8 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10180 views
The Power of Generative AI in Accelerating No Code Adoption.pdf by Saeed Al Dhaheri
The Power of Generative AI in Accelerating No Code Adoption.pdfThe Power of Generative AI in Accelerating No Code Adoption.pdf
The Power of Generative AI in Accelerating No Code Adoption.pdf
Saeed Al Dhaheri44 views
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」 by PC Cluster Consortium
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」
Discover Aura Workshop (12.5.23).pdf by Neo4j
Discover Aura Workshop (12.5.23).pdfDiscover Aura Workshop (12.5.23).pdf
Discover Aura Workshop (12.5.23).pdf
Neo4j20 views
"Package management in monorepos", Zoltan Kochan by Fwdays
"Package management in monorepos", Zoltan Kochan"Package management in monorepos", Zoltan Kochan
"Package management in monorepos", Zoltan Kochan
Fwdays37 views
Adopting Karpenter for Cost and Simplicity at Grafana Labs.pdf by MichaelOLeary82
Adopting Karpenter for Cost and Simplicity at Grafana Labs.pdfAdopting Karpenter for Cost and Simplicity at Grafana Labs.pdf
Adopting Karpenter for Cost and Simplicity at Grafana Labs.pdf
MichaelOLeary8213 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu474 views
Measurecamp Brussels - Synthetic data.pdf by Human37
Measurecamp Brussels - Synthetic data.pdfMeasurecamp Brussels - Synthetic data.pdf
Measurecamp Brussels - Synthetic data.pdf
Human37 27 views
LLMs in Production: Tooling, Process, and Team Structure by Aggregage
LLMs in Production: Tooling, Process, and Team StructureLLMs in Production: Tooling, Process, and Team Structure
LLMs in Production: Tooling, Process, and Team Structure
Aggregage65 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays37 views
AIM102-S_Cognizant_CognizantCognitive by PhilipBasford
AIM102-S_Cognizant_CognizantCognitiveAIM102-S_Cognizant_CognizantCognitive
AIM102-S_Cognizant_CognizantCognitive
PhilipBasford23 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE85 views
Bronack Skills - Risk Management and SRE v1.0 12-3-2023.pdf by ThomasBronack
Bronack Skills - Risk Management and SRE v1.0 12-3-2023.pdfBronack Skills - Risk Management and SRE v1.0 12-3-2023.pdf
Bronack Skills - Risk Management and SRE v1.0 12-3-2023.pdf
ThomasBronack31 views
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」 by PC Cluster Consortium
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」

Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final

Editor's Notes

  1. Let’s look at some code…
  2. The sample application… How to test Greeting? And to test the if (English)?
  3. Let’s look at some code…
  4. Very small change… Dependency Injection!
  5. Let’s look at some code…
  6. When using mock objects we do ‘Setup, Exercise and verify
  7. say again “When using mock objects we do ‘Setup, Exercise and verify”