SlideShare a Scribd company logo
Improving our server side testing
methods
Unit Testing
• Unit not well defined
• Dependency on other classes
• Small change breaks tests across several projects
• Purpose not always obvious
• Many tests only assert that the result is not null
• Asserting guards – not expectations
• Tests are not always easy to read
• Poor naming conventions
• Not always well structured
• Dependencies on test superclasses
• Not alone in these issues – companies fail in getting testing right
The Problem – Summary of issues with
server unit tests
• A “Unit” is a java class
• Most classes which perform operations should be unit tested
• Exceptions:-
• Model objects
• DAO – persistence managers
• Cover all methods and paths
• Dependencies should be mocked
• Mockito
• PowerMockito
What is a Unit
• Describes specification
• Code maintainability
• Improves the design of the code
• Documents the code
• Reduces bugs
• Identifies regressive behaviour
• Helps peer review process of code
Why Unit Test?
• DAMP (Descriptive And Meaningful Phrases)
• Useful for unit testing
• Promotes the readability of the code
• Prioritises readability over duplication of code
• Duplication is usually localised
• DRY (Don't repeat yourself)
• Useful for production code
• Remove duplication
• Every concept has a single authoritative representation in code
• Reduces risk of change if only in one place
DAMP vs DRY – Paradigm Shift
“So, why is duplication more acceptable in tests?
Tests often contain inherent duplication because they are testing the same thing
over and over again, only with slightly different input values or setup code.
However, unlike production code, this duplication is usually isolated only to the
scenarios within a single test fixture/file. Because of this, the duplication is minimal
and obvious, which means it poses less risk to the project than other types of
duplication.
Furthermore, removing this kind of duplication reduces the readability of the tests.
The details that were previously duplicated in each test are now hidden away in
some new method or class. To get the full picture of the test, you now have to
mentally put all these pieces back together.
Therefore, since test code duplication often carries less risk, and promotes
readability, its easy to see how it is considered acceptable.
As a principle, favour DRY in production code, favour DAMP in test code. While
both are equally important, with a little wisdom you can tip the balance in your
favour.”
- Chris Edwards - Stackoverflow
DAMP vs DRY – Paradigm Shift
• Each test should look as simple as possible
• The name should be meaningful
• If the name contains either “And” or “Or” –
split the test into two tests
• Split up tests which use if/else
• Test only one thing per test
• Variable names: camel case plain English – not
abbreviations.
• Avoid using literal values
What Unit Tests Should Look Like?
• Assert what we are expecting – not null pointers
• Use builders to construct test data
• Testing exceptions should be done with
annotations
e.g. @Test(expected =
AirpointInternalException.class)and not
using try/catch fail()
• Don’t make over use of setup methods
What Unit Tests Should Look Like? (cont…)
• Use a template to separate test setup, execution
and exceptions. E.g.:
// given
Setup
// when
Do something
// then
assert
What Unit Tests Should Look Like? (cont…)
“Now this is not the end. It is not
even the beginning of the end. But it
is, perhaps, the end of the
beginning.”
- Winston Churchill
Comments and Questions

More Related Content

What's hot

Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
Roy Osherove
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
David Paluy
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guy_davis
 
Test driven development
Test driven developmentTest driven development
Test driven development
Marcelo Cure
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
Excella
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
Jon Kruger
 
SCJP Preparation Tips
SCJP Preparation TipsSCJP Preparation Tips
SCJP Preparation Tips
Ganesh P
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
 
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
Christian Hujer
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partner
Kevin Poorman
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
Eyal Kenig
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
Naresh Jain
 
Agile Testing Days
Agile Testing DaysAgile Testing Days
Agile Testing Days
Marcin Czenko
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
Andrei Sebastian Cîmpean
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
MaslowB
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Samnang Chhun
 
Lập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven developmentLập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven development
Anh Lê
 
Unit testing
Unit testingUnit testing
Unit testing
PiXeL16
 

What's hot (20)

Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
SCJP Preparation Tips
SCJP Preparation TipsSCJP Preparation Tips
SCJP Preparation Tips
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
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
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partner
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Agile Testing Days
Agile Testing DaysAgile Testing Days
Agile Testing Days
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Lập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven developmentLập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven development
 
Unit testing
Unit testingUnit testing
Unit testing
 

Similar to Unit Testing

Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
Ganesh Kondal
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Md. Enamul Haque Chowdhury
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
Pekka Klärck
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
Bill Buchan
 
Clean code
Clean codeClean code
Clean code
Simon Sönnby
 
Testing with laravel
Testing with laravelTesting with laravel
Testing with laravel
Derek Binkley
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012
Alan Christensen
 
Introduction to Testing and TDD
Introduction to Testing and TDDIntroduction to Testing and TDD
Introduction to Testing and TDD
Sarah Dutkiewicz
 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
Sergei Kukharev
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
MaslowB
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
L&T Technology Services Limited
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Sahar Nofal
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Avinash Kadam
 
Tests that talk
Tests that talkTests that talk
Tests that talk
James McGivern
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
TDD
TDDTDD
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
Mark Baker
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
Mayank Srivastava
 
Testing recipes
Testing recipesTesting recipes

Similar to Unit Testing (20)

Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
Clean code
Clean codeClean code
Clean code
 
Testing with laravel
Testing with laravelTesting with laravel
Testing with laravel
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012
 
Introduction to Testing and TDD
Introduction to Testing and TDDIntroduction to Testing and TDD
Introduction to Testing and TDD
 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Tests that talk
Tests that talkTests that talk
Tests that talk
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
TDD
TDDTDD
TDD
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 
Testing recipes
Testing recipesTesting recipes
Testing recipes
 

More from Adam Birr

Church Websites
Church WebsitesChurch Websites
Church Websites
Adam Birr
 
Power of cleansing - Gospel of Mark 1 - Adam Birr
Power of cleansing - Gospel of Mark 1 - Adam BirrPower of cleansing - Gospel of Mark 1 - Adam Birr
Power of cleansing - Gospel of Mark 1 - Adam Birr
Adam Birr
 
Stages of spiritual growth - spiritual childhood
Stages of spiritual growth - spiritual childhoodStages of spiritual growth - spiritual childhood
Stages of spiritual growth - spiritual childhood
Adam Birr
 
Stages of spiritual growth - spiritual infancy
Stages of spiritual growth - spiritual infancyStages of spiritual growth - spiritual infancy
Stages of spiritual growth - spiritual infancy
Adam Birr
 
Real life discipleship - intro
Real life discipleship  - introReal life discipleship  - intro
Real life discipleship - intro
Adam Birr
 
You and Me forever - Glasgow Church of Christ
You and Me forever - Glasgow Church of ChristYou and Me forever - Glasgow Church of Christ
You and Me forever - Glasgow Church of Christ
Adam Birr
 
With your Soul
With your SoulWith your Soul
With your Soul
Adam Birr
 
Spirit of 2012 spark grant - This is Ruchill community website
Spirit of 2012 spark grant - This is Ruchill community websiteSpirit of 2012 spark grant - This is Ruchill community website
Spirit of 2012 spark grant - This is Ruchill community website
Adam Birr
 
Be Merciful - Glasgow church of Christ
Be Merciful - Glasgow church of ChristBe Merciful - Glasgow church of Christ
Be Merciful - Glasgow church of Christ
Adam Birr
 
Gospel legacy - Paolo Ugolini
Gospel legacy - Paolo UgoliniGospel legacy - Paolo Ugolini
Gospel legacy - Paolo Ugolini
Adam Birr
 
Gospel Unity - Paolo Ugolini
Gospel Unity - Paolo UgoliniGospel Unity - Paolo Ugolini
Gospel Unity - Paolo Ugolini
Adam Birr
 
Gospel purity - Paolo Ugolini
Gospel purity - Paolo UgoliniGospel purity - Paolo Ugolini
Gospel purity - Paolo Ugolini
Adam Birr
 
Deploying Enterprise Cordova Windows Phone Apps
Deploying Enterprise Cordova Windows Phone AppsDeploying Enterprise Cordova Windows Phone Apps
Deploying Enterprise Cordova Windows Phone Apps
Adam Birr
 
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
Adam Birr
 
Ephesians 2 - christ and relationships - Paolo Ugolini
Ephesians 2 - christ and relationships - Paolo UgoliniEphesians 2 - christ and relationships - Paolo Ugolini
Ephesians 2 - christ and relationships - Paolo Ugolini
Adam Birr
 
Ephesians 1 - every spiritual blessing
Ephesians 1 - every spiritual blessingEphesians 1 - every spiritual blessing
Ephesians 1 - every spiritual blessing
Adam Birr
 
Coversion of Saul
Coversion of SaulCoversion of Saul
Coversion of Saul
Adam Birr
 
The grace of salvation
The grace of salvationThe grace of salvation
The grace of salvation
Adam Birr
 
What is Truth? - by John Oakes
What is Truth? - by John OakesWhat is Truth? - by John Oakes
What is Truth? - by John Oakes
Adam Birr
 
The Case For Christ
The Case For ChristThe Case For Christ
The Case For Christ
Adam Birr
 

More from Adam Birr (20)

Church Websites
Church WebsitesChurch Websites
Church Websites
 
Power of cleansing - Gospel of Mark 1 - Adam Birr
Power of cleansing - Gospel of Mark 1 - Adam BirrPower of cleansing - Gospel of Mark 1 - Adam Birr
Power of cleansing - Gospel of Mark 1 - Adam Birr
 
Stages of spiritual growth - spiritual childhood
Stages of spiritual growth - spiritual childhoodStages of spiritual growth - spiritual childhood
Stages of spiritual growth - spiritual childhood
 
Stages of spiritual growth - spiritual infancy
Stages of spiritual growth - spiritual infancyStages of spiritual growth - spiritual infancy
Stages of spiritual growth - spiritual infancy
 
Real life discipleship - intro
Real life discipleship  - introReal life discipleship  - intro
Real life discipleship - intro
 
You and Me forever - Glasgow Church of Christ
You and Me forever - Glasgow Church of ChristYou and Me forever - Glasgow Church of Christ
You and Me forever - Glasgow Church of Christ
 
With your Soul
With your SoulWith your Soul
With your Soul
 
Spirit of 2012 spark grant - This is Ruchill community website
Spirit of 2012 spark grant - This is Ruchill community websiteSpirit of 2012 spark grant - This is Ruchill community website
Spirit of 2012 spark grant - This is Ruchill community website
 
Be Merciful - Glasgow church of Christ
Be Merciful - Glasgow church of ChristBe Merciful - Glasgow church of Christ
Be Merciful - Glasgow church of Christ
 
Gospel legacy - Paolo Ugolini
Gospel legacy - Paolo UgoliniGospel legacy - Paolo Ugolini
Gospel legacy - Paolo Ugolini
 
Gospel Unity - Paolo Ugolini
Gospel Unity - Paolo UgoliniGospel Unity - Paolo Ugolini
Gospel Unity - Paolo Ugolini
 
Gospel purity - Paolo Ugolini
Gospel purity - Paolo UgoliniGospel purity - Paolo Ugolini
Gospel purity - Paolo Ugolini
 
Deploying Enterprise Cordova Windows Phone Apps
Deploying Enterprise Cordova Windows Phone AppsDeploying Enterprise Cordova Windows Phone Apps
Deploying Enterprise Cordova Windows Phone Apps
 
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
Ephesians 2 3 - Reconciled with each other - Paolo Ugolini - Glasgow Church o...
 
Ephesians 2 - christ and relationships - Paolo Ugolini
Ephesians 2 - christ and relationships - Paolo UgoliniEphesians 2 - christ and relationships - Paolo Ugolini
Ephesians 2 - christ and relationships - Paolo Ugolini
 
Ephesians 1 - every spiritual blessing
Ephesians 1 - every spiritual blessingEphesians 1 - every spiritual blessing
Ephesians 1 - every spiritual blessing
 
Coversion of Saul
Coversion of SaulCoversion of Saul
Coversion of Saul
 
The grace of salvation
The grace of salvationThe grace of salvation
The grace of salvation
 
What is Truth? - by John Oakes
What is Truth? - by John OakesWhat is Truth? - by John Oakes
What is Truth? - by John Oakes
 
The Case For Christ
The Case For ChristThe Case For Christ
The Case For Christ
 

Unit Testing

  • 1. Improving our server side testing methods Unit Testing
  • 2. • Unit not well defined • Dependency on other classes • Small change breaks tests across several projects • Purpose not always obvious • Many tests only assert that the result is not null • Asserting guards – not expectations • Tests are not always easy to read • Poor naming conventions • Not always well structured • Dependencies on test superclasses • Not alone in these issues – companies fail in getting testing right The Problem – Summary of issues with server unit tests
  • 3. • A “Unit” is a java class • Most classes which perform operations should be unit tested • Exceptions:- • Model objects • DAO – persistence managers • Cover all methods and paths • Dependencies should be mocked • Mockito • PowerMockito What is a Unit
  • 4. • Describes specification • Code maintainability • Improves the design of the code • Documents the code • Reduces bugs • Identifies regressive behaviour • Helps peer review process of code Why Unit Test?
  • 5. • DAMP (Descriptive And Meaningful Phrases) • Useful for unit testing • Promotes the readability of the code • Prioritises readability over duplication of code • Duplication is usually localised • DRY (Don't repeat yourself) • Useful for production code • Remove duplication • Every concept has a single authoritative representation in code • Reduces risk of change if only in one place DAMP vs DRY – Paradigm Shift
  • 6. “So, why is duplication more acceptable in tests? Tests often contain inherent duplication because they are testing the same thing over and over again, only with slightly different input values or setup code. However, unlike production code, this duplication is usually isolated only to the scenarios within a single test fixture/file. Because of this, the duplication is minimal and obvious, which means it poses less risk to the project than other types of duplication. Furthermore, removing this kind of duplication reduces the readability of the tests. The details that were previously duplicated in each test are now hidden away in some new method or class. To get the full picture of the test, you now have to mentally put all these pieces back together. Therefore, since test code duplication often carries less risk, and promotes readability, its easy to see how it is considered acceptable. As a principle, favour DRY in production code, favour DAMP in test code. While both are equally important, with a little wisdom you can tip the balance in your favour.” - Chris Edwards - Stackoverflow DAMP vs DRY – Paradigm Shift
  • 7. • Each test should look as simple as possible • The name should be meaningful • If the name contains either “And” or “Or” – split the test into two tests • Split up tests which use if/else • Test only one thing per test • Variable names: camel case plain English – not abbreviations. • Avoid using literal values What Unit Tests Should Look Like?
  • 8. • Assert what we are expecting – not null pointers • Use builders to construct test data • Testing exceptions should be done with annotations e.g. @Test(expected = AirpointInternalException.class)and not using try/catch fail() • Don’t make over use of setup methods What Unit Tests Should Look Like? (cont…)
  • 9. • Use a template to separate test setup, execution and exceptions. E.g.: // given Setup // when Do something // then assert What Unit Tests Should Look Like? (cont…)
  • 10. “Now this is not the end. It is not even the beginning of the end. But it is, perhaps, the end of the beginning.” - Winston Churchill Comments and Questions