SlideShare a Scribd company logo
Unit testing Vocabulary, styles, toys  ____________________________
____________________________ Vocabulary […] I'm a pretty lazy person and am prepared to work quite hard in order to avoid work. (Martin Fowler – The Value of Self Testing code) self validating (tests check their own results) fast easy (no long tutorials before being able to use) integrated extendable J U nit framework JUnit framework has become Widely Used
____________________________ Vocabulary “ Unit tests are tests written with JUnit” (naive, simplistic,  false ) Unit Tests Integration, Functional, Acceptance Tests ->  also written with JUnit   If Unit tests cannot be defined via it’s Unit Testing Framework, what are they? Unit Testing :  - tests that perform a verification of a unit of code (article)    - “a unit test has no dependencies” (article) - a method of testing that verifies individual units of source code are working properly (wikipedia) Integration testing : - the part of software testing in which individual software modules are combined and tested together as a group.
____________________________ Vocabulary Unit Tests & not just tests “ One of the challenges of unit testing is to sufficiently  decouple  the multiple units of code that make up an application so that each unit can be tested individually.”   Unit Tests -> tend to become mini-integration tests!  Why should I Care? Testing simple functionality (ex: new methods) in  Integration Tests can lead to: Fragile Tests Unrepeatable Tests …  Maintenance Nightmare  
____________________________ Styles Non trivial tests:  Large number of dependencies Awkward collaborations Complex Objects    How to isolate the units of code? Using Test Doubles Test Doubles :  Dummy   -  objects are passed around but never actually used. Usually they are just used to fill parameter lists . Fake   -   objects actually have working implementations, but usually take some shortcut which makes them not suitable for production Stubs   -   provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Mocks   -   objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
____________________________ Styles What:  Order : must fill itself from Warehouse If Warehouse contains the product quantity requested by Order: - Order becomes filled, Warehouse quantity adjusted. If Warehouse does not contain necessary quantity: -Order does not become filled. Warehouse unchanged.   Example
____________________________ Styles Regular test
____________________________ Styles How do we test a mail was sent?! Stubs With a  Stub :
“ Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.” (Alice In Wonderland, Lewis Carroll) ____________________________ Styles Mocks
____________________________ Styles Mock test: jMock
____________________________ Styles We’re late!
____________________________ Vocabulary “ The end justifies the means” State-based testing Traditional (Classical)  Unit Testing . Combines  Stubs with Real Objects Failing Tests   –  A collaborator may lead to multiple tests failed   . Unnecessary code   – getters to expose state used only in tests.   Setup in a different place  - a stub may be shared, it’s returned values not visible in the test Sometimes stubs may be  hard to implement  by hand   – how do we stub a  HttpServletRequest ? A  java.io.File? What do we put in constructors? …
____________________________ Vocabulary “ OOP should be Behavior Oriented Programming” Interaction-based testing verifies   the  behavior  of a unit . the only real class is the SUT (system under test) Failing Tests   –  A problem at a Unit fails only Unit’s test. Unnecessary code   – don’t need state accessors.   Setup in setup  - a stub returned values are visible in test. Stubs easy to be fabricated. In TDD -> leads to good design (Interface discovery, Tell Don’t Ask) … .
____________________________ Vocabulary What should I use? Some code is very stateful, and is best tested with a state-based test.  . Other code is more stateless and can be fully tested only with an interaction-based style Design phase  – mocks may help  Maybe a better question: which style will be most effective for a particular piece of code?!
____________________________ Vocabulary Good Unit Tests “ Uncle Bob” suggests the  F.I.R.S.T  acronym in “Clean Code” to describe what well written (clean) unit tests should look like : F ast -  they should run quickly. This encourages you to run them often.  (framework)  I ndependent -  they should not depend on each other. It should be possible to run the tests in any order.   R epeatable -  it should be possible to run them in any environment. They should be environment independent.  S elf-Validating -  they should either pass or fail.   (framework) T imely -  they should be written in a timely manner i.e. just before the production code is written.
____________________________ Toys Mockito
questions? ____________________________ http :// martinfowler.com/articles/mocksArentStubs.html http://code.google.com/p/mockito/ http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html Martin Fowler: “ Refactoring . Improving the design of existing code ” http://blogs.thoughtworks.com/ Robert “Uncle Bob” Martin: “ Clean Code : A Handbook of Agile Software Craftsmanship” http://mockobjects.com/ http://benpryor.com/blog/2007/01/16/state-based-vs-interaction-based-unit-testing/

More Related Content

PPTX
An Introduction to unit testing
PPTX
ELW_Symantec_WriterTraining_Three Tutorials_28Jan2011
PPT
Xp Day 080506 Unit Tests And Mocks
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Practical unit testing tips
PDF
Unit Testing Done Right
PPTX
Battle of The Mocking Frameworks
PPTX
Mock driven development using .NET
An Introduction to unit testing
ELW_Symantec_WriterTraining_Three Tutorials_28Jan2011
Xp Day 080506 Unit Tests And Mocks
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Practical unit testing tips
Unit Testing Done Right
Battle of The Mocking Frameworks
Mock driven development using .NET

What's hot (20)

PPTX
Building unit tests correctly with visual studio 2013
PPTX
Unit Testing
PDF
Unit Testing Fundamentals
PPTX
Moq presentation
PPTX
Unit testing - the hard parts
PPTX
Understanding Unit Testing
PDF
Unit Testing 101
PPTX
Roy Osherove TDD From Scratch
PPTX
Unit Testing
PDF
Clean Unit Test Patterns
PPTX
Unit testing
PPTX
Unit Testing And Mocking
PPTX
Unit Testing (C#)
PPTX
Unit Testing Concepts and Best Practices
PPTX
An Introduction to Unit Testing
ODP
Embrace Unit Testing
PDF
Unit Test + Functional Programming = Love
PPT
Unit testing
PDF
Devday2016 real unittestingwithmockframework-phatvu
PDF
Unit testing (workshop)
Building unit tests correctly with visual studio 2013
Unit Testing
Unit Testing Fundamentals
Moq presentation
Unit testing - the hard parts
Understanding Unit Testing
Unit Testing 101
Roy Osherove TDD From Scratch
Unit Testing
Clean Unit Test Patterns
Unit testing
Unit Testing And Mocking
Unit Testing (C#)
Unit Testing Concepts and Best Practices
An Introduction to Unit Testing
Embrace Unit Testing
Unit Test + Functional Programming = Love
Unit testing
Devday2016 real unittestingwithmockframework-phatvu
Unit testing (workshop)
Ad

Similar to Unit Testing (20)

PPTX
Mocking
PPTX
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
PPTX
Unit Testing Full@
PPTX
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
PPTX
Automated Testing but like for PowerShell (April 2012)
PPT
Quality Software With Unit Test
PPTX
Type mock isolator
PDF
Testing and TDD - KoJUG
PDF
Test Driven Development - Workshop
PDF
Unit Testing & TDD Training for Mobile Apps
PPTX
Testing 101
PPTX
Testing & should i do it
PPTX
Type mock isolator
PPTX
Presentation
PDF
Unit testing - An introduction
PPTX
Unit Testing Android Applications
PDF
Unit Testing
PPTX
Getting started with Test Driven Development - Ferdous Mahmud Shaon
PPTX
Test driven development
PDF
Win at life with unit testing
Mocking
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Unit Testing Full@
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
Automated Testing but like for PowerShell (April 2012)
Quality Software With Unit Test
Type mock isolator
Testing and TDD - KoJUG
Test Driven Development - Workshop
Unit Testing & TDD Training for Mobile Apps
Testing 101
Testing & should i do it
Type mock isolator
Presentation
Unit testing - An introduction
Unit Testing Android Applications
Unit Testing
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Test driven development
Win at life with unit testing
Ad

Recently uploaded (20)

PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PPT
Chapter four Project-Preparation material
PPTX
Probability Distribution, binomial distribution, poisson distribution
PPTX
5 Stages of group development guide.pptx
PDF
COST SHEET- Tender and Quotation unit 2.pdf
DOCX
Euro SEO Services 1st 3 General Updates.docx
PDF
A Brief Introduction About Julia Allison
PDF
Nidhal Samdaie CV - International Business Consultant
PDF
Business model innovation report 2022.pdf
DOCX
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PPTX
ICG2025_ICG 6th steering committee 30-8-24.pptx
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PDF
Unit 1 Cost Accounting - Cost sheet
PPTX
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
PPTX
HR Introduction Slide (1).pptx on hr intro
PPTX
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
PDF
Laughter Yoga Basic Learning Workshop Manual
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
How to Get Funding for Your Trucking Business
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Chapter four Project-Preparation material
Probability Distribution, binomial distribution, poisson distribution
5 Stages of group development guide.pptx
COST SHEET- Tender and Quotation unit 2.pdf
Euro SEO Services 1st 3 General Updates.docx
A Brief Introduction About Julia Allison
Nidhal Samdaie CV - International Business Consultant
Business model innovation report 2022.pdf
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
ICG2025_ICG 6th steering committee 30-8-24.pptx
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Unit 1 Cost Accounting - Cost sheet
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
HR Introduction Slide (1).pptx on hr intro
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
Laughter Yoga Basic Learning Workshop Manual
Roadmap Map-digital Banking feature MB,IB,AB
How to Get Funding for Your Trucking Business

Unit Testing

  • 1. Unit testing Vocabulary, styles, toys ____________________________
  • 2. ____________________________ Vocabulary […] I'm a pretty lazy person and am prepared to work quite hard in order to avoid work. (Martin Fowler – The Value of Self Testing code) self validating (tests check their own results) fast easy (no long tutorials before being able to use) integrated extendable J U nit framework JUnit framework has become Widely Used
  • 3. ____________________________ Vocabulary “ Unit tests are tests written with JUnit” (naive, simplistic, false ) Unit Tests Integration, Functional, Acceptance Tests -> also written with JUnit If Unit tests cannot be defined via it’s Unit Testing Framework, what are they? Unit Testing : - tests that perform a verification of a unit of code (article) - “a unit test has no dependencies” (article) - a method of testing that verifies individual units of source code are working properly (wikipedia) Integration testing : - the part of software testing in which individual software modules are combined and tested together as a group.
  • 4. ____________________________ Vocabulary Unit Tests & not just tests “ One of the challenges of unit testing is to sufficiently decouple the multiple units of code that make up an application so that each unit can be tested individually.” Unit Tests -> tend to become mini-integration tests! Why should I Care? Testing simple functionality (ex: new methods) in Integration Tests can lead to: Fragile Tests Unrepeatable Tests … Maintenance Nightmare 
  • 5. ____________________________ Styles Non trivial tests: Large number of dependencies Awkward collaborations Complex Objects  How to isolate the units of code? Using Test Doubles Test Doubles : Dummy - objects are passed around but never actually used. Usually they are just used to fill parameter lists . Fake - objects actually have working implementations, but usually take some shortcut which makes them not suitable for production Stubs - provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Mocks - objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
  • 6. ____________________________ Styles What: Order : must fill itself from Warehouse If Warehouse contains the product quantity requested by Order: - Order becomes filled, Warehouse quantity adjusted. If Warehouse does not contain necessary quantity: -Order does not become filled. Warehouse unchanged. Example
  • 8. ____________________________ Styles How do we test a mail was sent?! Stubs With a Stub :
  • 9. “ Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.” (Alice In Wonderland, Lewis Carroll) ____________________________ Styles Mocks
  • 12. ____________________________ Vocabulary “ The end justifies the means” State-based testing Traditional (Classical) Unit Testing . Combines Stubs with Real Objects Failing Tests – A collaborator may lead to multiple tests failed . Unnecessary code – getters to expose state used only in tests. Setup in a different place - a stub may be shared, it’s returned values not visible in the test Sometimes stubs may be hard to implement by hand – how do we stub a HttpServletRequest ? A java.io.File? What do we put in constructors? …
  • 13. ____________________________ Vocabulary “ OOP should be Behavior Oriented Programming” Interaction-based testing verifies the behavior of a unit . the only real class is the SUT (system under test) Failing Tests – A problem at a Unit fails only Unit’s test. Unnecessary code – don’t need state accessors. Setup in setup - a stub returned values are visible in test. Stubs easy to be fabricated. In TDD -> leads to good design (Interface discovery, Tell Don’t Ask) … .
  • 14. ____________________________ Vocabulary What should I use? Some code is very stateful, and is best tested with a state-based test. . Other code is more stateless and can be fully tested only with an interaction-based style Design phase – mocks may help Maybe a better question: which style will be most effective for a particular piece of code?!
  • 15. ____________________________ Vocabulary Good Unit Tests “ Uncle Bob” suggests the F.I.R.S.T acronym in “Clean Code” to describe what well written (clean) unit tests should look like : F ast - they should run quickly. This encourages you to run them often. (framework) I ndependent - they should not depend on each other. It should be possible to run the tests in any order. R epeatable - it should be possible to run them in any environment. They should be environment independent. S elf-Validating - they should either pass or fail. (framework) T imely - they should be written in a timely manner i.e. just before the production code is written.
  • 17. questions? ____________________________ http :// martinfowler.com/articles/mocksArentStubs.html http://code.google.com/p/mockito/ http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html Martin Fowler: “ Refactoring . Improving the design of existing code ” http://blogs.thoughtworks.com/ Robert “Uncle Bob” Martin: “ Clean Code : A Handbook of Agile Software Craftsmanship” http://mockobjects.com/ http://benpryor.com/blog/2007/01/16/state-based-vs-interaction-based-unit-testing/