SlideShare a Scribd company logo
Just	
  Mock	
  It	
  
Mocks	
  and	
  Stubs	
  
Presented by Gavin Pickin
Slides: http://gpickin.com/itb2014/
Website: http://gpickin.com
Twitter: @gpickin
Introduc)on	
  
•  Follow	
  along	
  with	
  resources	
  from	
  
h6p://gpickin.com/itb2014/	
  
	
  
•  If	
  you	
  want	
  to	
  find	
  out	
  more	
  about	
  me,	
  my	
  
website	
  has	
  more	
  than	
  enough	
  informa)on	
  
20	
  Second	
  Agenda	
  
•  Unit	
  Tes)ng	
  -­‐	
  Recap	
  
•  What	
  is	
  Mocking	
  	
  
•  What	
  is	
  a	
  Mock	
  /	
  Stub	
  
•  Why	
  Mock	
  
•  Let’s	
  look	
  at	
  some	
  Mocking	
  
Unit	
  Tes)ng	
  
“unit	
  tes)ng	
  is	
  a	
  soOware	
  verifica)on	
  and	
  
valida)on	
  method	
  in	
  which	
  a	
  programmer	
  tests	
  
if	
  individual	
  units	
  of	
  source	
  code	
  are	
  fit	
  for	
  use.	
  
A	
  unit	
  is	
  the	
  smallest	
  testable	
  part	
  of	
  an	
  
applica)on”	
  
-­‐	
  wikipedia	
  
Unit	
  Tes)ng	
  
•  Can	
  improve	
  code	
  quality	
  -­‐>	
  quick	
  error	
  
discovery	
  
•  Code	
  confidence	
  via	
  immediate	
  verifica)on	
  
•  Can	
  expose	
  high	
  coupling	
  
•  Will	
  encourage	
  refactoring	
  to	
  produce	
  >	
  
testable	
  code	
  
•  Remember:	
  Tes)ng	
  is	
  all	
  about	
  behavior	
  and	
  
expecta)ons	
  
Bugs	
  Hurt	
  
Bugs	
  Hurt	
  
•  Bugs	
  hurt	
  –	
  the	
  later	
  in	
  the	
  process,	
  the	
  
harder	
  to	
  fix.	
  
•  Test	
  Early	
  and	
  OOen	
  
– Find	
  them	
  before	
  they	
  rot	
  your	
  founda)on	
  
•  Testable	
  Code	
  is	
  Maintainable	
  Code	
  
Unit	
  Tes)ng	
  Basics	
  
Test	
  Driven	
  Development	
  
Important	
  Tests	
  
•  Unit	
  Tes)ng	
  
– Test	
  behavior	
  of	
  individual	
  objects	
  
•  Integra)on	
  Tes)ng	
  
– Test	
  En)re	
  Applica)on	
  from	
  Top	
  Down	
  
•  UI	
  verifica)on	
  tes)ng	
  
– Verifica)on	
  via	
  HTML/Visual	
  elements	
  
Important	
  Tes)ng	
  Tools	
  
•  TestBox	
  (Run	
  BDD	
  and	
  MXUnit	
  style)	
  
•  IDE	
  -­‐	
  CF	
  Builder	
  /	
  Eclipse	
  
•  Mocking	
  Framework	
  
•  ANT	
  
•  Jenkins,	
  Bamboo,	
  Teamcity,	
  other	
  Cis	
  
•  Selenium	
  
•  Jmeter	
  or	
  Webstress	
  Tool,	
  Apache	
  AB	
  	
  
What	
  is	
  Mocking?	
  
According	
  to	
  Merriam	
  Webster	
  
•  to	
  laugh	
  at	
  or	
  make	
  fun	
  of	
  (someone	
  or	
  
something)	
  especially	
  by	
  copying	
  an	
  ac)on	
  or	
  
a	
  way	
  of	
  behaving	
  or	
  speaking	
  
•  to	
  imitate	
  (as	
  a	
  mannerism)	
  closely	
  	
  
What	
  is	
  Mocking?	
  
"A	
  mock	
  object	
  is	
  an	
  object	
  that	
  takes	
  the	
  place	
  
of	
  a	
  ‘real’	
  object	
  in	
  such	
  a	
  way	
  that	
  makes	
  
tes)ng	
  easier	
  and	
  more	
  meaningful,	
  or	
  in	
  some	
  
cases,	
  possible	
  at	
  all"	
  
by	
  Sco6	
  Bain	
  -­‐	
  Emergent	
  Design	
  
Mocks	
  
Mocks	
  
Mocks	
  
Mocks	
  
Stub	
  Object	
  
•  A	
  stub	
  is	
  an	
  empty	
  container,	
  that	
  represents	
  
an	
  Object.	
  
•  This	
  can	
  be	
  useful	
  for	
  represen)ng	
  CFCs	
  and	
  
Objects	
  that	
  haven’t	
  been	
  wri6en	
  yet.	
  
Why	
  use	
  Mocking	
  
•  Isolate	
  your	
  SUT	
  -­‐>	
  SoOware	
  Under	
  Test	
  
•  To	
  build	
  against	
  interfaces	
  &	
  contracts	
  
•  Building	
  against	
  missing	
  integra)on	
  pieces	
  
•  To	
  control	
  data	
  and	
  expecta)ons	
  
•  Mock	
  components	
  whose	
  behavior	
  is	
  
undesirable	
  or	
  hard	
  to	
  control	
  
Why	
  Mock?	
  
•  How	
  do	
  you	
  test	
  when	
  helper	
  components	
  
that	
  are	
  not	
  built	
  yet?	
  
•  How	
  do	
  you	
  do	
  controlled	
  excep)ons?	
  
•  How	
  do	
  you	
  test	
  &	
  control	
  external	
  API	
  calls?	
  
•  How	
  do	
  you	
  control	
  results	
  from	
  ColdFusion	
  
tags	
  or	
  func)ons?	
  
•  How	
  do	
  you	
  control	
  network	
  connec)ons?	
  Do	
  
you	
  pull	
  the	
  network	
  plug?	
  
Why	
  Mock?	
  
How	
  do	
  you	
  test	
  code	
  like	
  this?	
  
	
  
<cfdirectory	
  ac)on=”list”	
  directory=”#arguments.path#”	
  name=”qResults”>	
  
<ck6p	
  url=”#arguments.urlPath#”	
  results=”qResults”>	
  
<cfmail	
  to=”#to#”	
  from=”#from#”	
  subject=”#subject#”>#content#</cfmail>	
  
<cfquery	
  />	
  
func)on	
  init(){	
  
var	
  helper	
  =	
  new	
  Helper();	
  
}	
  
private	
  func)on	
  getData(){	
  return	
  data;	
  }	
  
The	
  COLD	
  hard	
  truth	
  –	
  It	
  can	
  Hurt	
  
•  Tes)ng	
  some	
  code	
  is	
  hard,	
  or	
  almost	
  
impossible.	
  	
  
•  Refactor	
  it	
  
•  Mock	
  it	
  
•  Test	
  It	
  
•  With	
  CI,	
  you	
  can	
  forget	
  about	
  it	
  
	
  Un)l	
  it	
  breaks	
  
Refactor	
  to	
  make	
  it	
  Mockable	
  
•  Original:	
  
<cfdirectory	
  ac)on=”list”	
  directory=”/myapp/path”	
  name=”qResults”>	
  
•  Refactored	
  
<cffunc)on	
  name=”getFiles”	
  output=”false”	
  returnType=”query”>	
  
	
  	
  	
  	
  <cfargument	
  name=”path”>	
  
	
  	
  	
  	
  <cfset	
  var	
  qResults	
  =	
  “”>	
  
	
  	
  	
  	
  <cfdirectory	
  ac)on=”list”	
  directory=”#arguments.path#”	
  name=”qResults”>	
  
	
  	
  	
  	
  ...	
  Process	
  Here	
  ...	
  
	
  	
  	
  	
  <cfreturn	
  qResults>	
  
</cffunc)on>	
  
Normal	
  View	
  of	
  your	
  Service	
  
Mock/Test	
  View	
  of	
  your	
  Service	
  
What	
  do	
  we	
  Mock?	
  
•  Excep)ons	
  
•  Data	
  
•  CFC	
  and	
  Objects	
  
•  Methods	
  in	
  our	
  CFCs	
  
•  Proper)es	
  in	
  our	
  CFCs	
  
Mocking	
  Framework	
  
•  Introducing	
  
What	
  can	
  MockBox	
  do?	
  
•  Mock	
  Objects	
  with	
  or	
  without	
  implementa)ons	
  
•  Mock	
  methods	
  &	
  proper)es	
  in	
  any	
  scope	
  
•  Create	
  Stub	
  Objects	
  -­‐>	
  Non-­‐existent	
  objects	
  
•  Mock	
  excep)ons	
  
•  Mock	
  arguments	
  to	
  results	
  
•  Logging	
  &	
  Debugging	
  
•  Verifica)on	
  methods	
  
•  State	
  Machine	
  Results	
  
Sepng	
  up	
  MockBox	
  
•  Standalone	
  Version	
  (FW/1,	
  Fusebox,	
  Model	
  
Glue,	
  Home	
  Brewed)	
  
mockBox	
  =	
  
createObject(“component”,”mockBox.system.tes)ng.MockBox”).init();	
  
	
  
•  Running	
  from	
  inside	
  ColdBox	
  (outside	
  of	
  TestBox)	
  
mockBox	
  =	
  
createObject(“component”,”coldbox.system.tes)ng.MockBox”).init();	
  
Using	
  MockBox	
  
•  MockBox	
  does	
  its	
  magic	
  dynamically,	
  adding	
  /	
  
decora)ng	
  CFCs	
  with	
  necessary	
  methods.	
  	
  
•  CreateMock()	
  
user	
  =	
  mockBox.createMock(“model.User”);	
  
•  CreateEmptyMock()	
  
dao	
  =	
  mockBox.createEmptyMock(“model.UserDAO”);	
  
•  PrepareMock()	
  
mockBox.prepareMock(	
  service	
  );	
  
Using	
  MockBox	
  -­‐	
  Stubs	
  
•  CreateStub()	
  
–  Create	
  a	
  simple	
  empty	
  Stub	
  
–  Pass	
  an	
  Implements	
  to	
  get	
  a	
  Mock	
  Interface	
  
–  Pass	
  an	
  Extends	
  to	
  get	
  a	
  Mock	
  with	
  Inheritance	
  
	
  
nonExistentService	
  =	
  mockBox.createStub();	
  
mockInterface	
  =	
  mockBox.createStub(implements=”model.ICache”);	
  
mockInheritance	
  =	
  mockbox.createStub(extends=”model.SecurityService”);	
  
Injected	
  Methods	
  
$()	
  
•  Argument	
  
–  method	
  
–  returns	
  
–  preserveReturnType	
  
–  throwExcep)on	
  
–  throwType	
  
–  throwDetail	
  
–  throwMessage	
  
–  callLogging	
  
// Cascaded mocks
mockUser.$(“isFound”,true).$(“isDirty”,true);
// Mock Exception
mockUser.
$(method=”save”,
throwsException=true,
throwType=”IllegalStateException”,
throwMessage=”Invalid User Data”);
// Mock Return Objects
mockRole = mockBox.createMock(“Role”);
service.$
(method=”getRole”,returns=mockRole);
Using	
  $()	
  
mockUser	
  =	
  mockBox.createEmptyMock(“model.User”).init();	
  
userService	
  =	
  mockBox.createMock(“model.UserService”).init();	
  
user	
  =	
  userService.$(“get”,	
  mockUser);	
  
	
  
//Technique	
  1	
  –	
  Constant	
  Result	
  
user.$(“getName”,	
  “Rose	
  Tyler”);	
  
	
  
//Technique	
  2	
  –	
  Rota)ng	
  Results	
  
user.$(“getName”).$results(“Rose	
  Tyler”,	
  “Amelia	
  Pond”,	
  “River	
  Song”);	
  
	
  
Using	
  $args()	
  
•  Results	
  based	
  on	
  the	
  Arguments	
  Passed	
  
•  Must	
  be	
  chained	
  via	
  Results.	
  
	
  
//	
  Call	
  to	
  Mock	
  
if(	
  dao.getSepng(“userAudit”)	
  ){	
  
startAudit(	
  dao.getSepng(“auditTables”)	
  );	
  
};	
  
	
  
//	
  Mocking	
  Calls	
  
dao.$(“getSepng”).$args(“userAudit”).$results(true);	
  
dao.$(“getSepng”).$args(“auditTables”).$results(“user,order,	
  
Using	
  $args()	
  
•  Arguments	
  are	
  smart	
  like	
  normal	
  func)ons	
  
–  Posi)onal	
  Arguments	
  
saveUser(”Rose”,”Tyler”);	
  
–  Named	
  Arguments	
  
saveUser(fname=”Rose”,lname=”Tyler”);	
  
–  Argument	
  Collec)ons	
  
data	
  =	
  {	
  
	
  fname	
  =	
  “Rose”,	
  lname	
  =	
  “Tyler”	
  
	
  };	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  saveUser(argumentCollec)on=data);	
  
	
  
$results()	
  
•  Your	
  results	
  can	
  be	
  	
  
–  Constant	
  
–  Or	
  Sequenced	
  
•  Your	
  results	
  will	
  loop	
  over	
  and	
  over.	
  
•  Argument	
  Based	
  Results	
  can	
  also	
  be	
  repe))ve	
  in	
  a	
  
sequence.	
  
$property()	
  
•  Mock	
  any	
  property	
  on	
  any	
  scope	
  
•  Great	
  for	
  sepngs	
  and	
  dependency	
  injec)on	
  mocking	
  
//	
  Mock	
  a	
  sepng	
  on	
  the	
  variables	
  scope	
  
service.$property(“cacheAc)ve”,”variables”,true);	
  
	
  
//	
  Mock	
  a	
  file	
  u)lity	
  object	
  
mockU)l	
  =	
  mockbox.createEmptyMock(“u)l.FileU)ls”);	
  
service.$property(“fileU)l”,”variables”,	
  mockU)l);	
  
	
  
//	
  Mock	
  in	
  the	
  variables.instance	
  scope	
  path	
  
service.$property(“isDirty”,”instance”,true);	
  
Verifica)on	
  Methods	
  
* Verification methods return boolean so they can be asserted
Verifica)on	
  Methods	
  
func)on	
  testVerifyCallCount(){	
  
	
  test.$("displayData",queryNew(''));	
  
	
  assertTrue(	
  test.$never()	
  );	
  
	
  assertTrue(	
  test.$never(“displayData”)	
  );	
  
	
  test.displayData();	
  
	
  assertFalse(	
  test.$)mes(1,”displayData”)	
  );	
  
	
  assertFalse(	
  test.$once(”displayData”)	
  );	
  
	
  test.displayData();	
  
	
  assertEquals(true,	
  test.$verifyCallCount(2));	
  
}	
  
Verifica)on	
  Methods	
  
	
  
func)on	
  testMockMethodCallCount(){	
  
	
  test.$("displayData",queryNew(''));	
  
	
  test.$("getLuis",1);	
  
	
  assertEquals(0,	
  test.$count("displayData")	
  );	
  
	
  assertEquals(-­‐1,	
  test.$count("displayData2")	
  );	
  
}	
  
Lets	
  look	
  at	
  a	
  Demo	
  
h6p://justmockit.local.com	
  
	
  
Code	
  available	
  at:	
  
h6ps://github.com/gpickin/justmockitdemo	
  
	
  
	
  
The	
  End	
  –	
  Thank	
  you	
  
•  Thanks	
  everyone	
  for	
  making	
  it	
  this	
  far	
  
•  All	
  of	
  the	
  materials	
  will	
  be	
  on	
  my	
  ITB	
  
presenta)on	
  Site:	
  	
  
h6p://www.gpickin.com/itb2014/	
  
•  Hit	
  me	
  up	
  on	
  twi6er	
  @gpickin	
  
•  Read	
  more	
  on	
  my	
  blog:	
  h6p://gpickin.com	
  
•  Check	
  out	
  my	
  CFO	
  Session	
  Thursday,	
  15th	
  
11:30am	
  –	
  12:30am	
  on	
  Lakes	
  Ballroom	
  C	
  

More Related Content

What's hot

Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Devfest 2019-slides
Devfest 2019-slidesDevfest 2019-slides
Devfest 2019-slides
Alan Richardson
 
Evil testers guide to technical testing
Evil testers guide to technical testingEvil testers guide to technical testing
Evil testers guide to technical testing
Alan Richardson
 
AATC2016: Exploratory testing an API
AATC2016: Exploratory testing an APIAATC2016: Exploratory testing an API
AATC2016: Exploratory testing an API
Maaret Pyhäjärvi
 
Low cost techniques for test doubles
Low cost techniques for test doublesLow cost techniques for test doubles
Low cost techniques for test doubles
Francisco Iglesias Gómez
 
TestWorksConf: Experience exploratory testing
TestWorksConf: Experience exploratory testingTestWorksConf: Experience exploratory testing
TestWorksConf: Experience exploratory testing
Maaret Pyhäjärvi
 
Practical Test Automation Deep Dive
Practical Test Automation Deep DivePractical Test Automation Deep Dive
Practical Test Automation Deep Dive
Alan Richardson
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
How to Improve Your Technical Test Ability - AADays 2015 Keynote
How to Improve Your Technical Test Ability - AADays 2015 KeynoteHow to Improve Your Technical Test Ability - AADays 2015 Keynote
How to Improve Your Technical Test Ability - AADays 2015 Keynote
Alan Richardson
 
If you want to automate, you learn to code
If you want to automate, you learn to codeIf you want to automate, you learn to code
If you want to automate, you learn to code
Alan Richardson
 
Automating Pragmatically - Testival 20190604
Automating Pragmatically - Testival 20190604Automating Pragmatically - Testival 20190604
Automating Pragmatically - Testival 20190604
Alan Richardson
 
Automating to Augment Testing
Automating to Augment TestingAutomating to Augment Testing
Automating to Augment Testing
Alan Richardson
 
Automating Strategically or Tactically when Testing
Automating Strategically or Tactically when TestingAutomating Strategically or Tactically when Testing
Automating Strategically or Tactically when Testing
Alan Richardson
 
Technology Based Testing
Technology Based TestingTechnology Based Testing
Technology Based Testing
Alan Richardson
 
Abstraction Layers Test Management Summit Faciliated Session 2014
Abstraction Layers Test Management Summit Faciliated Session 2014Abstraction Layers Test Management Summit Faciliated Session 2014
Abstraction Layers Test Management Summit Faciliated Session 2014
Alan Richardson
 
Technical Testing Webinar
Technical Testing WebinarTechnical Testing Webinar
Technical Testing Webinar
Alan Richardson
 
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
TestIstanbul May 2013 Keynote Experiences With Exploratory TestingTestIstanbul May 2013 Keynote Experiences With Exploratory Testing
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
Alan Richardson
 
Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014
Alan Richardson
 
Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020
Alan Richardson
 
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
Alan Richardson
 

What's hot (20)

Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Devfest 2019-slides
Devfest 2019-slidesDevfest 2019-slides
Devfest 2019-slides
 
Evil testers guide to technical testing
Evil testers guide to technical testingEvil testers guide to technical testing
Evil testers guide to technical testing
 
AATC2016: Exploratory testing an API
AATC2016: Exploratory testing an APIAATC2016: Exploratory testing an API
AATC2016: Exploratory testing an API
 
Low cost techniques for test doubles
Low cost techniques for test doublesLow cost techniques for test doubles
Low cost techniques for test doubles
 
TestWorksConf: Experience exploratory testing
TestWorksConf: Experience exploratory testingTestWorksConf: Experience exploratory testing
TestWorksConf: Experience exploratory testing
 
Practical Test Automation Deep Dive
Practical Test Automation Deep DivePractical Test Automation Deep Dive
Practical Test Automation Deep Dive
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
How to Improve Your Technical Test Ability - AADays 2015 Keynote
How to Improve Your Technical Test Ability - AADays 2015 KeynoteHow to Improve Your Technical Test Ability - AADays 2015 Keynote
How to Improve Your Technical Test Ability - AADays 2015 Keynote
 
If you want to automate, you learn to code
If you want to automate, you learn to codeIf you want to automate, you learn to code
If you want to automate, you learn to code
 
Automating Pragmatically - Testival 20190604
Automating Pragmatically - Testival 20190604Automating Pragmatically - Testival 20190604
Automating Pragmatically - Testival 20190604
 
Automating to Augment Testing
Automating to Augment TestingAutomating to Augment Testing
Automating to Augment Testing
 
Automating Strategically or Tactically when Testing
Automating Strategically or Tactically when TestingAutomating Strategically or Tactically when Testing
Automating Strategically or Tactically when Testing
 
Technology Based Testing
Technology Based TestingTechnology Based Testing
Technology Based Testing
 
Abstraction Layers Test Management Summit Faciliated Session 2014
Abstraction Layers Test Management Summit Faciliated Session 2014Abstraction Layers Test Management Summit Faciliated Session 2014
Abstraction Layers Test Management Summit Faciliated Session 2014
 
Technical Testing Webinar
Technical Testing WebinarTechnical Testing Webinar
Technical Testing Webinar
 
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
TestIstanbul May 2013 Keynote Experiences With Exploratory TestingTestIstanbul May 2013 Keynote Experiences With Exploratory Testing
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
 
Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014
 
Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020Automating Tactically vs Strategically SauceCon 2020
Automating Tactically vs Strategically SauceCon 2020
 
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
Test Bash Netherlands Alan Richardson "How to misuse 'Automation' for testing...
 

Similar to Just Mock It - Mocks and Stubs

CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
Ortus Solutions, Corp
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Tony Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Luis Goldster
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
James Wong
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Fraboni Ec
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Hoang Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Young Alista
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
Tim Duckett
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
Dror Helper
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
Camilo Lopes
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
DevDay.org
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...
Niels Frydenholm
 
Easy mock
Easy mockEasy mock
Easy mock
Srikrishna k
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
Yaron Karni
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Holger Grosse-Plankermann
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
Tamas Rev
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 

Similar to Just Mock It - Mocks and Stubs (20)

CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...
 
Easy mock
Easy mockEasy mock
Easy mock
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 

More from Gavin Pickin

Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Gavin Pickin
 
Containerizing ContentBox CMS
Containerizing ContentBox CMSContainerizing ContentBox CMS
Containerizing ContentBox CMS
Gavin Pickin
 
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
Gavin Pickin
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Gavin Pickin
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
BDD Testing and Automating from the trenches - Presented at Into The Box June...
BDD Testing and Automating from the trenches - Presented at Into The Box June...BDD Testing and Automating from the trenches - Presented at Into The Box June...
BDD Testing and Automating from the trenches - Presented at Into The Box June...
Gavin Pickin
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and ClientHow do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
How do I write Testable Javascript?
How do I write Testable Javascript?How do I write Testable Javascript?
How do I write Testable Javascript?
Gavin Pickin
 
Getting your Hooks into Cordova
Getting your Hooks into CordovaGetting your Hooks into Cordova
Getting your Hooks into Cordova
Gavin Pickin
 
Setting up your Multi Engine Environment - Apache Railo and ColdFusion
Setting up your Multi Engine Environment - Apache Railo and ColdFusionSetting up your Multi Engine Environment - Apache Railo and ColdFusion
Setting up your Multi Engine Environment - Apache Railo and ColdFusion
Gavin Pickin
 

More from Gavin Pickin (12)

Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin Pickin
 
Containerizing ContentBox CMS
Containerizing ContentBox CMSContainerizing ContentBox CMS
Containerizing ContentBox CMS
 
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
 
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
 
BDD Testing and Automating from the trenches - Presented at Into The Box June...
BDD Testing and Automating from the trenches - Presented at Into The Box June...BDD Testing and Automating from the trenches - Presented at Into The Box June...
BDD Testing and Automating from the trenches - Presented at Into The Box June...
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and ClientHow do I write Testable Javascript so I can Test my CF API on Server and Client
How do I write Testable Javascript so I can Test my CF API on Server and Client
 
How do I write Testable Javascript?
How do I write Testable Javascript?How do I write Testable Javascript?
How do I write Testable Javascript?
 
Getting your Hooks into Cordova
Getting your Hooks into CordovaGetting your Hooks into Cordova
Getting your Hooks into Cordova
 
Setting up your Multi Engine Environment - Apache Railo and ColdFusion
Setting up your Multi Engine Environment - Apache Railo and ColdFusionSetting up your Multi Engine Environment - Apache Railo and ColdFusion
Setting up your Multi Engine Environment - Apache Railo and ColdFusion
 

Recently uploaded

E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 

Recently uploaded (20)

E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 

Just Mock It - Mocks and Stubs

  • 1. Just  Mock  It   Mocks  and  Stubs   Presented by Gavin Pickin Slides: http://gpickin.com/itb2014/ Website: http://gpickin.com Twitter: @gpickin
  • 2. Introduc)on   •  Follow  along  with  resources  from   h6p://gpickin.com/itb2014/     •  If  you  want  to  find  out  more  about  me,  my   website  has  more  than  enough  informa)on  
  • 3. 20  Second  Agenda   •  Unit  Tes)ng  -­‐  Recap   •  What  is  Mocking     •  What  is  a  Mock  /  Stub   •  Why  Mock   •  Let’s  look  at  some  Mocking  
  • 4. Unit  Tes)ng   “unit  tes)ng  is  a  soOware  verifica)on  and   valida)on  method  in  which  a  programmer  tests   if  individual  units  of  source  code  are  fit  for  use.   A  unit  is  the  smallest  testable  part  of  an   applica)on”   -­‐  wikipedia  
  • 5. Unit  Tes)ng   •  Can  improve  code  quality  -­‐>  quick  error   discovery   •  Code  confidence  via  immediate  verifica)on   •  Can  expose  high  coupling   •  Will  encourage  refactoring  to  produce  >   testable  code   •  Remember:  Tes)ng  is  all  about  behavior  and   expecta)ons  
  • 7. Bugs  Hurt   •  Bugs  hurt  –  the  later  in  the  process,  the   harder  to  fix.   •  Test  Early  and  OOen   – Find  them  before  they  rot  your  founda)on   •  Testable  Code  is  Maintainable  Code  
  • 10. Important  Tests   •  Unit  Tes)ng   – Test  behavior  of  individual  objects   •  Integra)on  Tes)ng   – Test  En)re  Applica)on  from  Top  Down   •  UI  verifica)on  tes)ng   – Verifica)on  via  HTML/Visual  elements  
  • 11. Important  Tes)ng  Tools   •  TestBox  (Run  BDD  and  MXUnit  style)   •  IDE  -­‐  CF  Builder  /  Eclipse   •  Mocking  Framework   •  ANT   •  Jenkins,  Bamboo,  Teamcity,  other  Cis   •  Selenium   •  Jmeter  or  Webstress  Tool,  Apache  AB    
  • 12. What  is  Mocking?   According  to  Merriam  Webster   •  to  laugh  at  or  make  fun  of  (someone  or   something)  especially  by  copying  an  ac)on  or   a  way  of  behaving  or  speaking   •  to  imitate  (as  a  mannerism)  closely    
  • 13. What  is  Mocking?   "A  mock  object  is  an  object  that  takes  the  place   of  a  ‘real’  object  in  such  a  way  that  makes   tes)ng  easier  and  more  meaningful,  or  in  some   cases,  possible  at  all"   by  Sco6  Bain  -­‐  Emergent  Design  
  • 18. Stub  Object   •  A  stub  is  an  empty  container,  that  represents   an  Object.   •  This  can  be  useful  for  represen)ng  CFCs  and   Objects  that  haven’t  been  wri6en  yet.  
  • 19. Why  use  Mocking   •  Isolate  your  SUT  -­‐>  SoOware  Under  Test   •  To  build  against  interfaces  &  contracts   •  Building  against  missing  integra)on  pieces   •  To  control  data  and  expecta)ons   •  Mock  components  whose  behavior  is   undesirable  or  hard  to  control  
  • 20. Why  Mock?   •  How  do  you  test  when  helper  components   that  are  not  built  yet?   •  How  do  you  do  controlled  excep)ons?   •  How  do  you  test  &  control  external  API  calls?   •  How  do  you  control  results  from  ColdFusion   tags  or  func)ons?   •  How  do  you  control  network  connec)ons?  Do   you  pull  the  network  plug?  
  • 21. Why  Mock?   How  do  you  test  code  like  this?     <cfdirectory  ac)on=”list”  directory=”#arguments.path#”  name=”qResults”>   <ck6p  url=”#arguments.urlPath#”  results=”qResults”>   <cfmail  to=”#to#”  from=”#from#”  subject=”#subject#”>#content#</cfmail>   <cfquery  />   func)on  init(){   var  helper  =  new  Helper();   }   private  func)on  getData(){  return  data;  }  
  • 22. The  COLD  hard  truth  –  It  can  Hurt   •  Tes)ng  some  code  is  hard,  or  almost   impossible.     •  Refactor  it   •  Mock  it   •  Test  It   •  With  CI,  you  can  forget  about  it    Un)l  it  breaks  
  • 23. Refactor  to  make  it  Mockable   •  Original:   <cfdirectory  ac)on=”list”  directory=”/myapp/path”  name=”qResults”>   •  Refactored   <cffunc)on  name=”getFiles”  output=”false”  returnType=”query”>          <cfargument  name=”path”>          <cfset  var  qResults  =  “”>          <cfdirectory  ac)on=”list”  directory=”#arguments.path#”  name=”qResults”>          ...  Process  Here  ...          <cfreturn  qResults>   </cffunc)on>  
  • 24. Normal  View  of  your  Service  
  • 25. Mock/Test  View  of  your  Service  
  • 26. What  do  we  Mock?   •  Excep)ons   •  Data   •  CFC  and  Objects   •  Methods  in  our  CFCs   •  Proper)es  in  our  CFCs  
  • 28. What  can  MockBox  do?   •  Mock  Objects  with  or  without  implementa)ons   •  Mock  methods  &  proper)es  in  any  scope   •  Create  Stub  Objects  -­‐>  Non-­‐existent  objects   •  Mock  excep)ons   •  Mock  arguments  to  results   •  Logging  &  Debugging   •  Verifica)on  methods   •  State  Machine  Results  
  • 29. Sepng  up  MockBox   •  Standalone  Version  (FW/1,  Fusebox,  Model   Glue,  Home  Brewed)   mockBox  =   createObject(“component”,”mockBox.system.tes)ng.MockBox”).init();     •  Running  from  inside  ColdBox  (outside  of  TestBox)   mockBox  =   createObject(“component”,”coldbox.system.tes)ng.MockBox”).init();  
  • 30. Using  MockBox   •  MockBox  does  its  magic  dynamically,  adding  /   decora)ng  CFCs  with  necessary  methods.     •  CreateMock()   user  =  mockBox.createMock(“model.User”);   •  CreateEmptyMock()   dao  =  mockBox.createEmptyMock(“model.UserDAO”);   •  PrepareMock()   mockBox.prepareMock(  service  );  
  • 31. Using  MockBox  -­‐  Stubs   •  CreateStub()   –  Create  a  simple  empty  Stub   –  Pass  an  Implements  to  get  a  Mock  Interface   –  Pass  an  Extends  to  get  a  Mock  with  Inheritance     nonExistentService  =  mockBox.createStub();   mockInterface  =  mockBox.createStub(implements=”model.ICache”);   mockInheritance  =  mockbox.createStub(extends=”model.SecurityService”);  
  • 33. $()   •  Argument   –  method   –  returns   –  preserveReturnType   –  throwExcep)on   –  throwType   –  throwDetail   –  throwMessage   –  callLogging   // Cascaded mocks mockUser.$(“isFound”,true).$(“isDirty”,true); // Mock Exception mockUser. $(method=”save”, throwsException=true, throwType=”IllegalStateException”, throwMessage=”Invalid User Data”); // Mock Return Objects mockRole = mockBox.createMock(“Role”); service.$ (method=”getRole”,returns=mockRole);
  • 34. Using  $()   mockUser  =  mockBox.createEmptyMock(“model.User”).init();   userService  =  mockBox.createMock(“model.UserService”).init();   user  =  userService.$(“get”,  mockUser);     //Technique  1  –  Constant  Result   user.$(“getName”,  “Rose  Tyler”);     //Technique  2  –  Rota)ng  Results   user.$(“getName”).$results(“Rose  Tyler”,  “Amelia  Pond”,  “River  Song”);    
  • 35. Using  $args()   •  Results  based  on  the  Arguments  Passed   •  Must  be  chained  via  Results.     //  Call  to  Mock   if(  dao.getSepng(“userAudit”)  ){   startAudit(  dao.getSepng(“auditTables”)  );   };     //  Mocking  Calls   dao.$(“getSepng”).$args(“userAudit”).$results(true);   dao.$(“getSepng”).$args(“auditTables”).$results(“user,order,  
  • 36. Using  $args()   •  Arguments  are  smart  like  normal  func)ons   –  Posi)onal  Arguments   saveUser(”Rose”,”Tyler”);   –  Named  Arguments   saveUser(fname=”Rose”,lname=”Tyler”);   –  Argument  Collec)ons   data  =  {    fname  =  “Rose”,  lname  =  “Tyler”    };                        saveUser(argumentCollec)on=data);    
  • 37. $results()   •  Your  results  can  be     –  Constant   –  Or  Sequenced   •  Your  results  will  loop  over  and  over.   •  Argument  Based  Results  can  also  be  repe))ve  in  a   sequence.  
  • 38. $property()   •  Mock  any  property  on  any  scope   •  Great  for  sepngs  and  dependency  injec)on  mocking   //  Mock  a  sepng  on  the  variables  scope   service.$property(“cacheAc)ve”,”variables”,true);     //  Mock  a  file  u)lity  object   mockU)l  =  mockbox.createEmptyMock(“u)l.FileU)ls”);   service.$property(“fileU)l”,”variables”,  mockU)l);     //  Mock  in  the  variables.instance  scope  path   service.$property(“isDirty”,”instance”,true);  
  • 39. Verifica)on  Methods   * Verification methods return boolean so they can be asserted
  • 40. Verifica)on  Methods   func)on  testVerifyCallCount(){    test.$("displayData",queryNew(''));    assertTrue(  test.$never()  );    assertTrue(  test.$never(“displayData”)  );    test.displayData();    assertFalse(  test.$)mes(1,”displayData”)  );    assertFalse(  test.$once(”displayData”)  );    test.displayData();    assertEquals(true,  test.$verifyCallCount(2));   }  
  • 41. Verifica)on  Methods     func)on  testMockMethodCallCount(){    test.$("displayData",queryNew(''));    test.$("getLuis",1);    assertEquals(0,  test.$count("displayData")  );    assertEquals(-­‐1,  test.$count("displayData2")  );   }  
  • 42. Lets  look  at  a  Demo   h6p://justmockit.local.com     Code  available  at:   h6ps://github.com/gpickin/justmockitdemo      
  • 43. The  End  –  Thank  you   •  Thanks  everyone  for  making  it  this  far   •  All  of  the  materials  will  be  on  my  ITB   presenta)on  Site:     h6p://www.gpickin.com/itb2014/   •  Hit  me  up  on  twi6er  @gpickin   •  Read  more  on  my  blog:  h6p://gpickin.com   •  Check  out  my  CFO  Session  Thursday,  15th   11:30am  –  12:30am  on  Lakes  Ballroom  C